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?

Sumeet Kumar
  • 321
  • 4
  • 17
  • 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. – IcyBloom Dec 15 '20 at 06:54
  • @IcyBloom actual file is `menu.js` which has to be renamed as `menuOLD.js`. Renaming will perform if user performs `onClick` event. – Sumeet Kumar Dec 15 '20 at 06: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. – Keldan Chapman Dec 15 '20 at 07:01
  • @KeldanChapman kindly show me small demo or reference if possible. – Sumeet Kumar Dec 15 '20 at 07:11
  • There is no way to save a file from browser in this way. You need to perform this on the server side. – Abrar Hossain Dec 15 '20 at 07:58

1 Answers1

0

With Node.js

const fs = require('fs');

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

See here:

Keldan Chapman
  • 665
  • 5
  • 21