node.js Create, Delete, Change or Rename File or Folder

  Please read how to initialize npm from this post.


The reference is 'https://nodejs.org/dist/latest-v14.x/docs/api/fs.html'.


1. require


const fs = require('fs'); // Main requirement
const path = require('path');


if you want to know 'path', please check the link.


2. Create Folder


// Create folder
fs.mkdir(path.join(__dirname, '/example'), {}, function (err){

if(err) throw err;
console.log('Create Folder.....');

})






After you create the folder and run it another time, it will cause an error.


Because the folder is already exists. 


2. Create File

// Create file
fs.writeFile(path.join(__dirname, '/example', 'Hello_World.txt'), 'Hello World!', function (err){

if(err) throw err;
console.log('Create File.....');

})








3. Append File

// Create file
fs.writeFile(path.join(__dirname, '/example', 'Hello_World.txt'),
'Hello World!', function (err){

if(err) throw err;
console.log('Create File.....');

})

// Append File
fs.appendFile(path.join(__dirname, '/example', 'Hello_World.txt'),
'\nHello World Again!', function (err){

if(err) throw err;
console.log('append File.....');

})





// Create file
fs.writeFile(path.join(__dirname, '/example', 'Hello_World.txt'),
'Hello World!', function (err){

if(err) throw err;
console.log('Create File.....');
// Append File(CallBack)
fs.appendFile(path.join(__dirname, '/example', 'Hello_World.txt'),
     '\nHello World Again!', function (err){

if(err) throw err;
console.log('append File.....');

})

})


4. Read File

// Read File
fs.readFile(path.join(__dirname, '/example','Hello_World.txt'), 'utf8', function (err, data){

if(err) throw err;
console.log('Read File.....');
console.log(data);

});






4_1. Read File(synchronous)

let currentData = fs.readFileSync(path.join(__dirname, '/compoments', 'data.json'), 'utf8', (err, data) => {

if(err) throw err;
console.log('Read File.....');
console.log('data',data);
currentData = data;

});

console.log('currentData',currentData);




5. File Rename

// Rename File
const path1 = path.join(__dirname, '/example','Hello_World.txt');
const Rename_path2 = path.join(__dirname, '/example','Rename_Hello_World.txt');

fs.rename(path1, Rename_path2, function (err){

if(err) throw err;
console.log(`File rename ${path.basename(path1)} to ${path.basename(Rename_path2)}`);

});







6. Remove Folder

// Remove Folder
const Remove_path = path.join(__dirname, '/example');
fs.rmdir(Remove_path2, {recursive : true} , function(err) {

if(err) throw err;
console.log(`Folder deleted.....`);
        console.log(`path : ${Remove_path}`);

})




7. Remove File

// Remove File
const Remove_path = path.join(__dirname, '/example', 'Hello_World.txt');
fs.rm(Remove_path, {recursive : true} , function(err) {

if(err) throw err;
console.log(`file deleted.....`);
console.log(`path : ${Remove_path}`);

})









댓글

이 블로그의 인기 게시물

Lesson 12_1 프로퍼티 노드(Property Node)

DAQ로 전압 측정하기-2

Lesson 12_2 참조를 이용한 프로퍼티노드(Property Node)