【发布时间】:2018-11-02 15:50:09
【问题描述】:
我正在尝试在一个文件夹中运行检查,如果有任何其他文件而不是扩展名 - '.txt',则该文件夹将只显示一个警报
这是我的代码:
for (let file of files) {
const ext = path.extname(file);
console.log(typeof file)
if (ext != '.txt') {
console.log(ext)
console.log("The error should have popped up")
dialog.showErrorBox('Non TXT file detected', 'there is a non-txt file in the folder')
break
}
else {
console.log("Not Working")
}
}
我遇到的问题是说文件夹中有 10 个“.txt”文件,如果我将其中一个重命名为“.test”,我会收到 10 个警报。有没有办法在“dialog.showErrorBox”被调用后立即阻止 for 循环继续。
无论我更改哪个文件的扩展名,我都会收到相同数量的错误。
【问题讨论】:
-
你真的不应该依赖自动分号插入 (ASI):What are the rules for JavaScript's automatic semicolon insertion (ASI)?
-
你真的不应该依赖别人告诉你不要依赖自动分号插入 (ASI):What are the rules for JavaScript's automatic semicolon insertion (ASI)?。
-
尽管我对此并不完全确定,但我认为使用
!==而不是!=更好(并且正确..?)。也许这会导致你的问题:) -
我在这里制作了您的代码的简化版本:jsfiddle.net/tw7p31dv,您的代码似乎可以正常工作。你能再检查一下你的文件吗
-
break肯定会结束for..of循环,您以后要重新运行循环吗?使用当前代码,无论有多少具有不同扩展名的文件名,您都应该只得到一个错误。
标签: javascript node.js electron