Skip to content

Instantly share code, notes, and snippets.

@icai
Created March 20, 2024 03:48
Show Gist options
  • Save icai/80dcb01013eea51afe24dd45adae8d67 to your computer and use it in GitHub Desktop.
Save icai/80dcb01013eea51afe24dd45adae8d67 to your computer and use it in GitHub Desktop.
scan directory and show list
const fs = require('fs')
const path = require('path')
function readDir(dir, extFilter) {
let fileList = []
// Function to recursively read directories
function readDirectoryRecursive(directory) {
// Read the directory
fs.readdir(directory, (err, files) => {
if (err) {
console.error('Error reading directory:', err)
return
}
// Iterate through the files
files.forEach((file) => {
const filePath = path.join(directory, file)
// Check if it's a file
fs.stat(filePath, (err, stats) => {
if (err) {
console.error('Error stating file:', err)
return
}
if (stats.isFile()) {
// Check if the file matches the extension filter
if (!extFilter || path.extname(file) === extFilter) {
fileList.push(filePath)
}
} else if (stats.isDirectory()) {
// If it's a directory, recursively read it
readDirectoryRecursive(filePath)
}
})
})
})
}
// Start reading the directory recursively
readDirectoryRecursive(dir)
// Wait for all file stats to be checked
setTimeout(() => {
// Output the list of files as JSON
console.log(JSON.stringify(fileList, null, 2))
}, 1000) // Adjust timeout as needed
}
// Example usage:
readDir('./aftersale-pages', '.axml')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment