-
-
Save rizkiandrianto/7e336dc345e32b7376e1f91ab9cfb8da to your computer and use it in GitHub Desktop.
Node Js Directory remove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"); | |
var rmDir = function(dir, rmSelf) { | |
var files; | |
rmSelf = (rmSelf === undefined) ? true : rmSelf; | |
dir = dir + "/"; | |
try { files = fs.readdirSync(dir); } catch (e) { console.log("!Oops, directory not exist."); return; } | |
if (files.length > 0) { | |
files.forEach(function(x, i) { | |
if (fs.statSync(dir + x).isDirectory()) { | |
rmDir(dir + x); | |
} else { | |
fs.unlinkSync(dir + x); | |
} | |
}); | |
} | |
if (rmSelf) { | |
// check if user want to delete the directory ir just the files in this directory | |
fs.rmdirSync(dir); | |
} | |
} | |
// Example rmDir("file1") => delete directory with all files || rmDir("file1", false) => delete just the files in the directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment