Reputation: 111
How could I use javascript to copy C:\folderA\myfile.txt
to C:\folderB\myfile.txt
? I also have to check to make sure that myfile.txt
does not already exist in folderB
. And finally I have to then rename the new file from myfile.txt
to myfile.bak
.
I know javascript can't really be used on a local file system, but if it could be, how would I write this code as simply as possible?
Upvotes: 9
Views: 48300
Reputation: 1201
in browser side you cannot access local system files.But in server side you could do it as follows.
//copyfile.js
const fs = require('fs');
// destination will be created or overwritten by default.
fs.copyFile('C:\folderA\myfile.txt', 'C:\folderB\myfile.txt', (err) => {
if (err) throw err;
console.log('File was copied to destination');
});
nodejs has to be installed on your server and then run above script as follows
node copyfile.js
Upvotes: 20