copy file nodejs code example
Example 1: copy a file and paste with fs
const fs = require('fs');
fs.copyFile('source.txt', 'destination.txt', (err) => {
if (err) throw err;
console.log('source.txt was copied to destination.txt');
});
Example 2: node filesystem change directory of a file
var fs = require('fs-extra')
fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', function (err) {
if (err) return console.error(err)
console.log("success!")
})
Example 3: copy text from file to another file in javascript with fs
'use strict';
import fs from 'fs';
let fileContent = 'Anything what you want';
let message = fs.writeFileSync('message.txt', fileContent);
function copyContent(fileName: string, dest: string): boolean {
try {
fs.copyFileSync(fileName, dest);
console.log(dest);
return true;
} catch (err) {
return false;
}
}
console.log(copyContent('message.txt', 'destination.txt'));
Example 4: copy file javascript
const fs = require('fs');
fs.copyFile('C:\folderA\myfile.txt', 'C:\folderB\myfile.txt', (err) => {
if (err) throw err;
console.log('File was copied to destination');
});