I am working on a small assignment in typescript, currently I'm facing below mentioned issue and I was not able to figure out a way of achieving this.Any guidance or advises of below mentioned issue will be highly appreciated.
I have small task to copy a directory to another location in the file system,So currently I'm using fs-extra.copySync method in npm package, to copy directory from one location to another, but when I copy files need to exclude some file types (.xml, .txt).
So the issue is in the directory which I'm going to copy, if there's a sub folder the unallowed file types(.xml & .txt) will also get copied. So I tried below methods, but it's giving below error.
Cannot read property 'readFiles' of null
Methods I tried is mentioned below.
server.ts
function moveFilesAll()
{
var moveFrom = "./src";
var moveTo = "./Destination";
readFiles(moveFrom,moveTo);
}
function readFiles(moveFrom,moveTo)
{
fs.readdir(moveFrom, function (err, files) {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
files.forEach(function (file, index) {
console.log(file + " "+ index)
// Make one pass and make the file complete
var fromPath = path.join(moveFrom, file);
var toPath = path.join(moveTo, file);
fs.stat(fromPath, function (error, stat) {
if (error) {
console.error("Error stating file.", error);
return;
}
if (stat.isFile())
{
console.log("'%s' is a file.", fromPath);
console.log(path.extname(fromPath));
//files get copying here
if(path.extname(fromPath) =='.txt' || path.extname(fromPath) == '.xml' || path.extname(fromPath) == '.config'){
console.log("Unallowed file types");
}
else
{
console.log("---------------Files copying--------------------------");
fsExtra.copySync(fromPath, toPath);
console.log("copied from '%s' to '%s'. ", fromPath, toPath);
}
}
else if (stat.isDirectory())
{
console.log("=================Directory=============");
console.log("From path "+fromPath);
console.log("TO path "+toPath);
readFiles(fromPath,toPath);
console.log("'%s' is a directory.", fromPath);
}
});
})
})
}