move a file in javascript

To move a file using JavaScript, you can use the rename method from the fs module. Here's an example:

index.tsx
const fs = require('fs');

// old path of the file
const oldPath = '/path/to/old/file.txt';

// new path of the file
const newPath = '/path/to/new/file.txt';

// rename the file
fs.rename(oldPath, newPath, (error) => {
  if (error) {
    console.log(error);
  } else {
    console.log('File moved successfully!');
  }
});
322 chars
17 lines

This code renames the file located at 'path/to/old/file.txt' to 'path/to/new/file.txt'. If there is an error, it will be logged to the console. Otherwise, it will log 'File moved successfully!'.

related categories

gistlibby LogSnag