1
2 var fs = require('fs');
3
4fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
5
6 if (err) throw err;
7 console.log('File Renamed!');
8});
1//copy the $file to $dir2
2var copyFile = (file, dir2)=>{
3 //include the fs, path modules
4 var fs = require('fs');
5 var path = require('path');
6
7 //gets file name and adds it to dir2
8 var f = path.basename(file);
9 var source = fs.createReadStream(file);
10 var dest = fs.createWriteStream(path.resolve(dir2, f));
11
12 source.pipe(dest);
13 source.on('end', function() { console.log('Succesfully copied'); });
14 source.on('error', function(err) { console.log(err); });
15};
16
17//example, copy file1.htm from 'test/dir_1/' to 'test/'
18copyFile('./test/dir_1/file1.htm', './test/');
19