我第一次尝试遍历目录,但是stat
由于似乎缺乏权限,某些目录会抛出错误。
Error: EPERM: operation not permitted, stat 'K:\System Volume Information'
我想避免打电话stat
首先在给定的目录上,如果它会抛出错误,但我不知道该怎么做。
我尝试过完全忽略受保护的目录。然而,我遇到的所有问题都使用了正则表达式——根据我对 MacOS、Linux 以及 Windows 上的处理方式的有限了解——这些正则表达式似乎不适用于 Windows 环境。
我尝试在调用之前检查目录的读写权限stat
,但这似乎没有任何作用?
async function scanDirs(){
const
r = await fsp.readFile('./config.json', 'utf8'),
archives = JSON.parse(r).archives,
fs = require('fs'),
{ join } = require('path'),
traverse = async (dir) => {
try {
const perm = await fsp.access(dir, fs.constants.R_OK)
if (perm === undefined){
const stats = await fsp.stat(dir)
if (stats.isDirectory()){
const subfolders = await fsp.readdir(dir)
subfolders.forEach(path => {
const fullPath = join(dir, path)
traverse( fullPath )
})
}
}
}
catch (error){
console.error(error)
}
}
for (const dir of archives){
traverse(dir)
}
}
我也刚刚尝试检查给定文件夹的子级的权限,然后再将它们返回traverse
,但这也行不通。
for (const path of subfolders){
const
fullPath = join(dir, path),
perm = await fsp.access(fullPath, fs.constants.R_OK)
if (perm === undefined) traverse(fullPath)
}
任何帮助,将不胜感激。