node.js check file, folder path or concatenate path

 Please read how to initialize npm from this post.


1. require

// Reference : https://nodejs.org/dist/latest-v14.x/docs/api/path.html
const path = require('path');


2. Base File name

console.log("The Base File Path");
console.log(path.basename(__filename));

Print the current file name. use '.basename' to console.log the file name. The output type is string. '__filename' is the current file.




3. Directory name

console.log("The File Directory Path");
console.log(path.dirname(__filename));

Print the current file directory. use '.dirname' to console.log the file directory. The output type is string.


4. File Extension

console.log("The File Extension");
console.log(path.extname(__filename));

Print the current file extension. use '.extname' to console.log the file extension. The output type is string.




5. Create Path Object

console.log("\n\nThe File Whole Info");
console.log(path.parse(__filename));

Print the current file path information object. use '.parse' to console.log the file path object. The output type is object.



6. Concatenate

// ../test/Hello_World.txt
console.log("\n\nThe 'Hello_World.txt' File Path");
console.log(path.join(__dirname, 'test', 'Hello_World.txt'));

Print the concatenate string. This example print 'Hello_World.txt' path. Concatenate to 'test' folder and add 'Hello_World.txt'. The output type is string.









댓글

이 블로그의 인기 게시물

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

DAQ로 전압 측정하기-2

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