JS宏遍历目录里的文件——通过everything的http服务
wils
创作者俱乐部成员
一般我们在JS宏里,遍历目录的方法是使用Dir,大概这样
🔔 | function aa() { let f = Dir(`${ThisWorkbook.Path}\\*`) while (f) { console.log(f) f = Dir() } } |
如果需要包含子目录,就有点麻烦:
Dir的第二个参数要包含16,表示遍历目录
要用FileSystem.stat对象的isDirectory方法,找出目录
要改成递归写法,逐层遍历目录
可以尝试用everything.exe提供的http服务来搞,既灵活又快速
先开启服务,然后在JS宏里发fetch请求就好
🔔 | function bb() { const s = encodeURIComponent(`"${ThisWorkbook.Path}\\" `) fetch(`h t t p://localhost/?s=${s}&j=1`).then(r=>r.json()).then(j=>{ for (const i of j.results) { if (i.type === 'file') { console.log(i.name) } } }) } |
其中s是具体的查询,j=1是说返回json形式的结果
这样既不用写递归,又可以利用everything提供的各种强大查询方法,快速获得需要的文件列表