const { resolve } = require("path");
const prompt = require('prompt');
const fsPath = require('fs-path');
// Get files from Dir
const getFiles = dir => {
const stack = [resolve(dir)];
const files = [];
while (stack.length) {
dir = stack.pop();
fs.readdirSync(dir).forEach(item => {
const path = resolve(dir, item);
(fs.statSync(path).isDirectory() ? stack : files).push(path);
});
}
return files;
};
prompt.start();
// Prompt for Dir
prompt.get(['from', 'to'], async(err, res) => {
if (err) {
return console.log(err);
} else {
let fromFiles = await getFiles(String(res.from));
let toFiles = await getFiles(String(res.to));
let isEqual = JSON.stringify(fromFiles) == JSON.stringify(toFiles);
let out = res.to;
if (isEqual) {
return console.log('Both folders are same. Try it with an other folder!');
} else {
let dif = [];
// Check differences -> to path
await fromFiles.forEach(path => {
if (!toFiles.includes(path)) {
dif.push(path)
}
});
dif.forEach(difference => {
fsPath.copy(`${difference}`, `${out + difference.split(__dirname)[1]}`, (error) => {
if (error) {
console.log(error);
} else {
console.log('Successfully copied item.')
}
});
});
}
}
});
Hi Guys, in the code above i want to only copy files that dont exist in the 'toFiles' Folder.
But I have a problem, if I try to do it with the fsPath npm Package it gives out errors like this 'Error: ENOENT: no such file or directory'. Normally the package should create the parent folders if there are none. But it somehow doesnt work. Could anybody look after the code and try to fix it? Thanks very much.
question from:
https://stackoverflow.com/questions/65542108/cant-copy-files-to-unknown-directory-in-node-js 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…