0

I am having JavaScript file under menu directory in the root menu.js. I want to rename JavaScript file menu.js to menuOLD.js under same directory onClick. I would have googled and found small sample as :

function renamefile(){
         const myFile = new File(['hello-world'], 'my-file.txt');
         const myRenamedFile = new File([myFile], 'my-file-final-1-really.txt');
         
         console.log(myRenamedFile);
          
     }

I have checked it's output in Console and got below output: enter image description here

It's working.

But I would need to rename excatly menu.js file under menu directory. How should I do this?

5
  • Just for clarification, do you mean that issue is that the old file has to be exactly named menu.js or that the new file will be explicitly menuOLD.js? If possible, do explain your desired results again. Commented Dec 15, 2020 at 6:54
  • @IcyBloom actual file is menu.js which has to be renamed as menuOLD.js. Renaming will perform if user performs onClick event. Commented Dec 15, 2020 at 6:56
  • 1
    I don't think it is possible to interact with the filesystem in a browser environment (I could be wrong though), but if you are using Node.js, then the fs module will let you do that. Commented Dec 15, 2020 at 7:01
  • @KeldanChapman kindly show me small demo or reference if possible. Commented Dec 15, 2020 at 7:11
  • There is no way to save a file from browser in this way. You need to perform this on the server side. Commented Dec 15, 2020 at 7:58

1 Answer 1

0

With Node.js

const fs = require('fs');

fs.rename('menu.js', 'menuOLD.js', (err) => { 
  console.log(err);
});

See here:

Sign up to request clarification or add additional context in comments.

Comments

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.