nodejs-fs-utils
NodeJs FileSystem (FS) extra utilities
Methods
- rmdirs - remove a file or folder even it isn't empty ( accept specifying logic for symlinks )
- rmdirsSync - synchronous version of rmdirs
- mkdirs - creates a folder and it parent if it is needed ( accept specifying logic for symlinks )
- mkdirsSync - synchronous version of mkdirs
- emptyDir - remove contents of a dir
- emptyDirSync - synchronous version of emptyDir
- isEmpty - return if file is empty or not
- isEmptySync - synchronous version of isEmpty
- remove - remove file, directory or link
- removeSync - synchronous version of remove
symbolicLinks
- treat symbolic links as files ( default true
)skipErrors
- skip errors just log them ( default false
)
- fsize - count file or folder size, has additional options
symbolicLinks
- treat symbolic links as files ( default true
)countFolders
- counts and folder inode size ( default true
)countSymbolicLinks
- counts symbolic links inode size ( default true
)logErrors
- log all error in an array ( default false
)skipErrors
- skip errors just log them ( default false
)
- fsizeSync - synchronous version of fsize
- move - move files or folders
- moveSync - synchronous version of move
symlinksKeep
- specify how to treat symlinks
symlinksNormalize
- specify if is needed link normalizing
linkFiles
- for files linking instead of coping
symbolicLinks
- treat symbolic links as files ( default true
)countFolders
- counts and folder inode size ( default true
)countSymbolicLinks
- counts symbolic links inode size ( default true
)logErrors
- log all error in an array ( default false
)skipErrors
- skip errors just log them ( default false
)
- copy - copy files or folders
- copySync - synchronous version of copy
- walk - walk throuth files, folder and links ( advanced configurations )
- walkSync - synchronous version of walk
rmdirs
optional can be send fs module in "fs"
option, P.S. it removes link files or directories.
var fsUtils = require("nodejs-fs-utils");
fsUtils.rmdirs("test/folder", function (err) {
});
fsUtils.rmdirs("test/folder", function (err) {
}, {
symbolicLinks : false
});
fsUtils.rmdirs("test/folder", function (err) {
}, {
skipErrors : true
});
var fsUtils = require("nodejs-fs-utils");
fsUtils.rmdirsSync("test/folder");
fsUtils.rmdirsSync("test/folder", {
symbolicLinks : false
});
fsUtils.rmdirsSync("test/folder", {
skipErrors : true
});
emptyDir
remove contents of a directory
var fsUtils = require("nodejs-fs-utils");
fsUtils.emptyDir("test/folder", function (err) {
});
emptyDirSync
remove contents of a directory, synchronous
var fsUtils = require("nodejs-fs-utils");
fsUtils.emptyDirSync("test/folder");
isEmpty
checks if folder is empty
var fsUtils = require("nodejs-fs-utils");
fsUtils.isEmpty("test/folder", function (err, state) {
});
isEmptySync
checks if folder is empty, synchronous
var fsUtils = require("nodejs-fs-utils");
fsUtils.isEmptySync("test/folder");
mkdirs - build a directory tree
optional can be send fs module in "fs"
option
var fsUtils = require("nodejs-fs-utils");
fsUtils.mkdirs("newfolder/folder/subfolder", function (err) {
});
fsUtils.rmdirs("newfolder/folder/symbolic-link/subfolder", {
symbolicLinks : false
}, function (err) {
});
var fsUtils = require("nodejs-fs-utils");
fsUtils.mkdirs("newfolder/folder/subfolder");
fsUtils.rmdirs("newfolder/folder/symbolic-link/subfolder", {
symbolicLinks : false
});
remove
removing file or directories
similar / alias as rmdirs, click here to view.
removeSync
removing file or directories
similar / alias as rmdirs, click here to view.
fsize - advanced file size scan for links folders or files
optional can be send fs module in "fs"
option
var fsUtils = require("nodejs-fs-utils");
fsUtils.fsize("videos/video.mp4", function (err, size) {
});
fsUtils.fsize("newfolder/folder/subfolder", function (err, size) {
});
fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
symbolicLinks : false
}, function (err, size) {
});
fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true
}, function (err, size) {
});
fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true,
logErrors : true
}, function (err, size) {
});
fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
countFolders : false
}, function (err, size) {
});
fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
countSymbolicLinks : false
}, function (err, size) {
});
fsizeSync - file or folder size synchronous
var fsUtils = require("nodejs-fs-utils");
var size = fsUtils.fsizeSync("videos/video.mp4");
var size = fsUtils.fsizeSync("newfolder/folder/subfolder", function (err, size) {
});
var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", {
symbolicLinks : false
});
var config = { skipErrors : true };
var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", config);
if (config.errors.length) {
console.log("Error detected: ", config.errors[0])
}
var config = { skipErrors : true };
var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", config);
if (config.errors.length) {
console.log("Error detected: ", config.errors[0])
}
var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", {
countFolders : false
});
var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", {
countSymbolicLinks : false
});
move
move file of folders or links
options for move function:
symlinksNormalize
- specify how to treat symlinks
specify if is needed link normalizing
accepted values: "auto"
, "none"
, "relative"
, "absolute"
"auto"
, "none"
or "absolute"
- uses absolute path
"relative"
- uses relative paths for links
P.S "auto"
will be dynamic in future, will try to use relative if it is posiblesymlinksKeep
- specify if is needed to keep simplinks or to move files or folders
accepted values: "file", "directory", "all"linkFiles
- for files linking instead of moving
accepted values: "auto", "none", "relative", "absolute"symbolicLinks
- treat symbolic links as files ( default true
)countFolders
- counts and folder inode size ( default true
)countSymbolicLinks
- counts symbolic links inode size ( default true
)logErrors
- log all error in an array ( default false
)skipErrors
- skip errors just log them ( default false
)
P.S. if is true
the errors will be an array
move examples:
moveSync examples:
var fsUtils = require("nodejs-fs-utils");
fsUtils.move(__dirname + "/source", "./destination-path", function (err, cache) {
if (!err) {
console.log("Moved !");
} else {
console.error("Error", err)
}
});
fsUtils.move(__dirname + "/source", "./destination-path", function (errors, cache) {
if (!errors.length) {
console.log("Moved !");
} else {
errors.forEach(function (err) {
console.error("Error", err)
});
}
}, {
skipErrors : true
});
moveSync
synchronous version for move function
moveSync examples:
var fsUtils = require("nodejs-fs-utils");
var moveSync = fsUtils.moveSync;
moveSync(__dirname + "/source", "./destination-path", function (err, cache) {
if (!err) {
console.log("Moved !");
} else {
console.error("Error", err)
}
});
moveSync(__dirname + "/source", "./destination-path", function (errors, cache) {
if (!errors.length) {
console.log("Moved !");
} else {
errors.forEach(function (err) {
console.error("Error", err)
});
}
}, {
skipErrors : true
});
copy
copy file of folders or links
options for copy function:
symlinksNormalize
- specify how to treat symlinks
specify if is needed link normalizing
accepted values: "auto"
, "none"
, "relative"
, "absolute"
"auto"
, "none"
or "absolute"
- uses absolute path
"relative"
- uses relative paths for links
P.S "auto"
will be dynamic in future, will try to use relative if it is posiblesymlinksKeep
- specify if is needed to keep simplinks or to copy files or folders
accepted values: "file", "directory", "all"linkFiles
- for files linking instead of coping
accepted values: "auto", "none", "relative", "absolute"symbolicLinks
- treat symbolic links as files ( default true
)countFolders
- counts and folder inode size ( default true
)countSymbolicLinks
- counts symbolic links inode size ( default true
)logErrors
- log all error in an array ( default false
)skipErrors
- skip errors just log them ( default false
)
P.S. if is true
the errors will be an array
copy examples:
copySync examples:
var fsUtils = require("nodejs-fs-utils");
fsUtils.copy(__dirname + "/source", "./destination-path", function (err, cache) {
if (!err) {
console.log("Copied !");
} else {
console.error("Error", err)
}
});
fsUtils.copy(__dirname + "/source", "./destination-path", function (errors, cache) {
if (!errors.length) {
console.log("Copied !");
} else {
errors.forEach(function (err) {
console.error("Error", err)
});
}
}, {
skipErrors : true
});
fsUtils.copy(__dirname + "/source", "./destination-path", function (err, cache) {
if (!err) {
console.log("Files were linked !");
} else {
console.error("Error", err)
}
}, {
linkFiles : "relative"
});
copySync
synchronous version for copy function
copySync examples:
var fsUtils = require("nodejs-fs-utils");
var copySync = fsUtils.copySync;
copySync(__dirname + "/source", "./destination-path", function (err, cache) {
if (!err) {
console.log("Copied !");
} else {
console.error("Error", err)
}
});
copySync(__dirname + "/source", "./destination-path", function (errors, cache) {
if (!errors.length) {
console.log("Copied !");
} else {
errors.forEach(function (err) {
console.error("Error", err)
});
}
}, {
skipErrors : true
});
copySync(__dirname + "/source", "./destination-path", function (err, cache) {
if (!err) {
console.log("Files were linked !");
} else {
console.error("Error", err)
}
}, {
linkFiles : "relative"
});
walk - walk throuth files folder and links ( advanced configurations )
optional can be send fs module in "fs"
option
cache reference can be used for storing data while walking
var fsUtils = require("nodejs-fs-utils");
fsUtils.walk("./videos", function (err, path, stats, next, cache) {
next();
});
fsUtils.walk("./videos", function (err, path, stats, next, cache) {
next();
}, function (cache) {
console.log("Walk is finished");
});
fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
symbolicLinks : false
}, function (err, path, stats, next, cache) {
next();
});
fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true
}, function (err, path, stats, next, cache) {
next();
});
fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true,
logErrors : true
}, function (err, path, stats, next, cache) {
next();
}, function (cache) {
if (cache.errors.length) {
console.log("Errors: ", cache.errors);
} else {
console.log("No errors found");
}
});
walkSync - walk sync throuth files folder and links ( advanced configurations )
walkSync
has same api as walk
, but it is synchronous
var fsUtils = require("nodejs-fs-utils");
fsUtils.walkSync("./videos", function (err, path, stats, next, cache) {
next();
});
fsUtils.walkSync("./videos", function (err, path, stats, next, cache) {
next();
}, function (cache) {
console.log("Walk is finished");
});
fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
symbolicLinks : false
}, function (err, path, stats, next, cache) {
next();
});
fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true
}, function (err, path, stats, next, cache) {
next();
});
fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true,
logErrors : true
}, function (err, path, stats, next, cache) {
next();
}, function (cache) {
if (cache.errors.length) {
console.log("Errors: ", cache.errors);
} else {
console.log("No errors found");
}
});
walk - examples
getArray of folders in a array
var fsUtils = require("nodejs-fs-utils");
var folders = [];
fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true,
logErrors : true
}, function (err, path, stats, next, cache) {
if (!err && stats.isDirectory()) {
folders.push(path);
}
next();
});
remove folders with name "tmp"
var fsUtils = require("nodejs-fs-utils");
var fs = require("fs");
fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true,
logErrors : true
}, function (err, path, stats, next, cache) {
if (!err && stats.isDirectory() && path.match(/\/tmp$/)) {
fs.unlinkSync(path);
} else {
next();
}
});
fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
skipErrors : true,
logErrors : true,
stackPushEnd: true
}, function (err, path, stats, next, cache) {
if (!err && stats.isDirectory() && path.match(/\/tmp$/)) {
fs.unlinkSync(path);
cache.count++;
} else {
next();
}
}, function (cache) {
console.log("Finished")
});