Ami*_*ein 5 node.js node-webkit electron
我创建了一个NodeJS(电子)代码,用于读取特定目录和子目录中的所有文件.我不想使用太多的高清资源,这就是为什么我在文件夹之间使用5ms的延迟.
现在我的问题.我希望我的NODE过程停止吗?我希望能够在停止时继续.我该怎么办?
换句话说:如何在所有文件和文件夹中行走时保持当前状态的索引,这样我就可以从停止时继续遍历.
谢谢
我的代码:
var walkAll=function(options){
var x=0
walk(options.dir,function(){})
function walk(dir,callback) {
var files=fs.readdirSync(dir);
var stat;
async.eachSeries(files,function(file,next){
file=dir +'/' + file
if (dir.match(/Recycle/)) return next()
if (dir.match(/.git/)) return next()
if (dir.match(/node_modules/)) return next()
fs.lstat(file,function(err,stat){
if(err) return next()
if(stat.mode==41398) return next()
if (stat.isDirectory()) {
setTimeout(function(file){
walk(file,next)
}.bind(null,file),5)
}
else{
x++
if(false || x % 1000===0) console.log((new Date().valueOf()-start)/1000,x,file)
next()
}
})
},function(){
callback()
})
}
}
walkAll({
dir:'c:/',
delay:1000
});
Run Code Online (Sandbox Code Playgroud)
保留要访问的子目录列表,并在每次迭代时更新该列表。
walk以下示例中的函数采用前一个状态,并返回具有下一个状态的下一个子目录的文件。
您可以在停止进程之前保存状态,然后在重新启动时加载保存的状态以继续遍历。
function walk(state, readdir) {
let files = [], next = [];
while (state.length > 0) {
try {
const current = state.shift()
files = readdir(current).map(file => current + '/' + file)
next = state.concat(files)
break
} catch(e) {}
}
return [next, files]
}
function main() {
const {writeFileSync: writeFile, readdirSync: readdir} = require('fs')
const save = './walk.json'
let state
try {
state = require(save)
} catch(e) {}
if (!state || state.length < 1) state = ['.']
const [nextState, files] = walk(state, readdir)
console.log(files)
writeFile(save, JSON.stringify(nextState, null, 2))
}
main()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
253 次 |
| 最近记录: |