Tutorial
Display file list

Listing Files with the File System Module

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ Files Creation

Explain

Try to understand the problem statement

Problem Statement : Make files in a folder

  1. Create a folder named files in the root directory of your project.
💡

making a simple file is very easy just use the fs module and call the writeFileSync method and pass the file name and the data you want to write in the file as the parameters.

 const fs = require('fs');
 fs.writeFileSync('hello.txt', 'Hello World!');
  1. Now do like this
 const fs = require('fs');
 const path = require('path');
 const dirPath = path.join(__dirname, 'files');//__dirname will give the current directory name and files is the folder name
 
 //console.warn(dirPath);//this will print the path of the folder
 
 for (i = 0; i < 10; i++) {
 
 //fs.writeFileSync("hello.txt", "Hello World!");
 //this will create a file named hello.txt in the root directory and overwrite it if it already exists
 //So we need to add dirPath in the file name and make it dynamic like text1.txt,text2.txt,text3.txt etc
 
     fs.writeFileSync(dirPath + `/file${i}.txt`, `Hello World! ${i}`);
 
  //fs.writeFileSync(dirPath + `/file'+i+'.txt`, `Hello World!`+i);//this will also work
 }

Now run node index.js and check the files folder. You should see 10 files with the names file0.txt to file9.txt and the contents Hello World! X, where X is the current value of i.

Code Explanation

Step 1

const fs = require('fs');

  • The fs module is used to interact with the file system in Node.js.

Step 2

const path = require('path');

  • The path module provides utilities for working with file and directory paths.

Step 3

const dirPath = path.join(__dirname, 'files');

  • The __dirname variable is a special variable in Node.js that represents the directory name of the current module.
  • The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
  • In this case, we're joining the current directory name with a subdirectory called "files".

Step 4

for (i = 0; i < 10; i++) {

  • This is a for loop that will run 10 times.

Step 5

fs.writeFileSync(dirPath + `/file${i}.txt`, `Hello World! ${i}`);

  • The fs.writeFileSync() method writes data to a file, replacing the file if it already exists.
  • In this case, we're writing a file to the "files" subdirectory with a filename of "fileX.txt", where X is the current value of i.
  • The contents of each file will be "Hello World! X", where X is the current value of i.

Try to understand the problem statement

Problem Statement : We need to see the files in the folder

3 . We are going to use readdir

 fs.readdir(dirPath, (err, files) => {
    if (err) {
        console.error(err);
        return;
    }
    console.warn(files);
  });

Output

[ 'file0.txt',
 'file1.txt',
 'file2.txt',
 'file3.txt',
 'file4.txt',
 ]

Code Explanation

fs.readdir(dirPath, (err, files) - The fs.readdir() method reads the contents of a directory.

  • In this case, we're reading the contents of the "files" subdirectory.
  • The files parameter is an array of filenames in the directory.
  • The err parameter is an error object if an error occurred, or null if no error occurred

console.warn(files); - This will print the array of filenames to the console.

Try to understand the problem statement

Problem Statement : We don't want to see the files in the array format

  1. We are going to use the forEach method to iterate over the array and print the files
💡

forEach is a method that is used to iterate over an array.

Syntax

arr.forEach(abc => {
    console.warn(abc);
});

or

arr.forEach((abc, index) => {
    console.warn(index, abc);
});
fs.readdir(dirPath, (err, files) => {
    if (err) {
        console.error(err);
        return;
    }
    files.forEach(file => {
        console.warn(file);
    });
});

or

fs.readdir(dirPath, (err, files) => {
    if (err) {
        console.error(err);
        return;
    }
    files.forEach((file, index) => {
        console.warn(index, file);
    });
});

Output

file0.txt
file1.txt
file2.txt
file3.txt
...this will go on

Code Explanation

files.forEach(file => { - This is a forEach loop that will iterate over the array of filenames.

console.warn(file); - This will print the filename to the console.