Last active
June 5, 2023 14:33
-
-
Save ikenna/37fa024a59ddf83a98ee45eeea144ed0 to your computer and use it in GitHub Desktop.
Node script to ist files in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
function printFileNames(directory) { | |
fs.readdir(directory, (err, files) => { | |
if (err) { | |
console.error(`Error reading directory: ${err}`); | |
return; | |
} | |
files.forEach(file => { | |
const filePath = path.join(directory, file); | |
const stats = fs.statSync(filePath); | |
if (stats.isFile()) { | |
console.log(file); | |
} | |
}); | |
}); | |
} | |
// Usage example | |
const directory = 'path/to/directory'; | |
printFileNames(directory); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment