😆remove empty directories recursively (ok)
https://gist.github.com/jakub-g/5903dc7e4028133704a4
PreviousThay đổi tên ảnh hàng loạt bằng ảnh khác, Changename (ok)NextSử dụng nodejs và scss, js cấu trúc thư mục tốt (ok)
Last updated
https://gist.github.com/jakub-g/5903dc7e4028133704a4
Last updated
F:\Thien Hoa Account\RemoveEmpty\package-lock.json
{
"name": "RemoveEmpty",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "RemoveEmpty",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"fs": "^0.0.1-security",
"fs.promises": "^0.1.2"
}
},
"node_modules/fs": {
"version": "0.0.1-security",
"resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
"integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==",
"license": "ISC"
},
"node_modules/fs.promises": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/fs.promises/-/fs.promises-0.1.2.tgz",
"integrity": "sha512-LNfkXdN6EToumV455EbdJaNxqLmEFqKqNZVIEyI2whtdpIppTsne2fHWgx05s+B2Aif29Lhzdz0AaOXKLvMGsA==",
"engines": {
"node": ">=8.9"
}
}
}
}
F:\Thien Hoa Account\RemoveEmpty\remove.js
const fs = require('fs');
const path = require("path");
function getFiles(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir);
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) {
const name = `${dir}/${file}`
// Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) {
console.log(name);
cleanEmptyFoldersRecursively(name);
// If it is a directory, recursively call the getFiles function with the directory path and the files array
// getFiles(name, files)
} else {
// If it is a file, push the full path to the files array
files.push(name)
}
}
return files
}
function cleanEmptyFoldersRecursively(folder) {
var isDir = fs.statSync(folder).isDirectory();
if (!isDir) {
return;
}
var files = fs.readdirSync(folder);
if (files.length > 0) {
files.forEach(function(file) {
var fullPath = path.join(folder, file);
cleanEmptyFoldersRecursively(fullPath);
});
// re-evaluate files; after deleting subfolder
// we may have parent folder empty now
files = fs.readdirSync(folder);
}
if (files.length == 0) {
console.log("removing: ", folder);
fs.rmdirSync(folder);
return;
}
}
getFiles('files');