Answers for "nodejs rename file"

3

fs renaming files

const fs = require("fs")

fs.rename("./testfile.txt", "./newtestfile.txt", (err) => {
	if (err) console.log(err)
})
Posted by: Guest on November-22-2020
0

rename file in js

var fs = require('fs');
 
fs.rename('sample_old.txt', 'sample_new.txt', function (err) {
  if (err) throw err;
  console.log('File Renamed.');
});
Posted by: Guest on July-22-2020
0

rename file using node javascript

var fs = require('fs');

fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
  
  if (err) throw err;
  console.log('File Renamed!');
});
Posted by: Guest on July-18-2021
0

rename a file using node.js

const { join } = require('path');
const { readdirSync, renameSync } = require('fs');
const [dir, search, replace] = process.argv.slice(2);
const match = RegExp(search, 'g');
const files = readdirSync(dir);

files
  .filter(file => file.match(match))
  .forEach(file => {
    const filePath = join(dir, file);
    const newFilePath = join(dir, file.replace(match, replace));

    renameSync(filePath, newFilePath);
  });

// Usage
// node rename.js path/to/directory 'string-to-search' 'string-to-replace'
Posted by: Guest on August-04-2020
0

nodejs copy and rename file

//copy the $file to $dir2
var copyFile = (file, dir2)=>{
  //include the fs, path modules
  var fs = require('fs');
  var path = require('path');

  //gets file name and adds it to dir2
  var f = path.basename(file);
  var source = fs.createReadStream(file);
  var dest = fs.createWriteStream(path.resolve(dir2, f));

  source.pipe(dest);
  source.on('end', function() { console.log('Succesfully copied'); });
  source.on('error', function(err) { console.log(err); });
};

//example, copy file1.htm from 'test/dir_1/' to 'test/'
copyFile('./test/dir_1/file1.htm', './test/');
Posted by: Guest on September-25-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language