Node.js - file system

PHOTO EMBED

Wed Apr 06 2022 14:32:47 GMT+0000 (Coordinated Universal Time)

Saved by @monkeyDo

const fs = require('fs');
const path = require('path');

// Create folder
fs.mkdir(path.join(__dirname, '/test'), {}, err => {
    if (err) throw err;
    console.log('Folder created...');
});

// Create and write to file
fs.writeFile(path.join(__dirname, '/test', 'hello.txt'), 'Hello World!', err => {
    if (err) throw err;
    console.log('File written to...');
});

// Add text to an existing file - this will OVERRIDES the existing content in this file
fs.writeFile(path.join(__dirname, '/test', 'hello.txt'), ' I love Node.js', err => {
    if (err) throw err;
    console.log('File written to...');
});

// Append content inside a file
fs.appendFile(path.join(__dirname, '/test', 'hello.txt'), ' I love Node.js', err => {
    if (err) throw err;
    console.log('File written to...');
});

// Read file content
fs.readFile(path.join(__dirname, '/test', 'hello.txt'), 'utf8', (err, data) => {
    if (err) throw err;
    console.log(data);
});

// Rename file
fs.rename(path.join(__dirname, '/test', 'hello.txt'), path.join(__dirname, '/test', 'helloworld.txt'), (err) => {
    if (err) throw err;
    console.log('File renamed...');
});
content_copyCOPY