9

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?

7
  • Is this server-side JavaScript, i.e. can you use node.js (and the fs module) or do you need to run this from a client browser? Commented Aug 15, 2018 at 16:21
  • 3
    You can use Nodejs to access your local file system Commented Aug 15, 2018 at 16:21
  • 1
    I'm not familiar enough with node.js. I basically have to just write it in regular javascript and in a client browser. Commented Aug 15, 2018 at 16:24
  • @user2051533 client side you can't. Commented Aug 15, 2018 at 16:25
  • how about non-client side?? Commented Aug 15, 2018 at 16:26

1 Answer 1

20

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
Sign up to request clarification or add additional context in comments.

1 Comment

It is possible to access local system files in browsers that support the File System Access API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.