const fs = require('fs');
const path = require('path');
const directoryPath = '/path/to/your/directory';
// Step 1: Read the contents of the directory
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
// Step 2: Iterate through the list of files
files.forEach((file) => {
// Step 3: Get the full path of each file
const filePath = path.join(directoryPath, file);
// Step 4: Check if the path is a file or a directory
fs.stat(filePath, (err, stats) => {
if (err) {
console.error('Error getting file stats:', err);
return;
}
// Step 5: Print the file name and type (file or directory)
console.log(`${file} - ${stats.isDirectory() ? 'Directory' : 'File'}`);
});
});
});