// Prompt user for file names rl.question('Enter the name of the first file: ', (file1) => { rl.question('Enter the name of the second file: ', (file2) => { // Read content of file1 fs.readFile(file1, 'utf8', (err, data) => { if (err) { console.error('Error reading file1:', err); rl.close(); return; }
// Append content of file1 to file2 fs.appendFile(file2, data, (err) => { if (err) { console.error('Error appending to file2:', err); rl.close(); return; }
console.log(`Contents of ${file1} appended to ${file2} successfully!`); rl.close(); }); }); }); });
2 Comments
file2 content not appended only name of file can be appenede
ReplyDeleteconst readline = require('readline');
ReplyDeleteconst fs = require('fs');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Prompt user for file names
rl.question('Enter the name of the first file: ', (file1) => {
rl.question('Enter the name of the second file: ', (file2) => {
// Read content of file1
fs.readFile(file1, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file1:', err);
rl.close();
return;
}
// Append content of file1 to file2
fs.appendFile(file2, data, (err) => {
if (err) {
console.error('Error appending to file2:', err);
rl.close();
return;
}
console.log(`Contents of ${file1} appended to ${file2} successfully!`);
rl.close();
});
});
});
});