Skip to content

Instantly share code, notes, and snippets.

@ikenna
Last active June 5, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikenna/37fa024a59ddf83a98ee45eeea144ed0 to your computer and use it in GitHub Desktop.
Save ikenna/37fa024a59ddf83a98ee45eeea144ed0 to your computer and use it in GitHub Desktop.
Node script to ist files in a directory
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