Currently, I am working on a small TypeScript assignment and facing an issue that I can't seem to solve. Any guidance or advice on the problem mentioned below would be greatly appreciated.
The task at hand involves copying a directory from one location to another in the file system. I am utilizing the fs-extra.copySync method from the npm package to achieve this, however, I need to exclude certain file types (.xml, .txt) during the copy process.
The problem arises when there are sub-folders within the directory being copied, as the forbidden file types (.xml & .txt) also get copied along with them. I have tried different methods but encountered the following error:
Cannot read property 'readFiles' of null
The method I attempted is shown 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);
}
});
})
})
}