Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - Copy file from one directory to another

Tags:

javascript

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?

like image 463
user2051533 Avatar asked Aug 15 '18 16:08

user2051533


1 Answers

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
like image 192
Nuwan Attanayake Avatar answered Sep 21 '22 11:09

Nuwan Attanayake