-1

I am trying to run a check within a folder that will display only one alert if there is any other file than extension - '.txt'

Here is my code:

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")
  }
}

The issue that I am having is that say there were 10 ".txt" files in the folder, if I renamed one of them to ".test" I would receive 10 alerts. Is there a way to as soon as the "dialog.showErrorBox" has been called prevent to for loop for continuing.

I receive the same amount of errors no matter which file i change the extension for.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
Luke
  • 48
  • 8
  • 3
    You really shouldn't rely on automatic semicolon insertion (ASI): [What are the rules for JavaScript's automatic semicolon insertion (ASI)?](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) – Andreas Nov 02 '18 at 15:55
  • 2
    You really shouldn't rely on people telling you not to rely on automatic semicolon insertion (ASI): [What are the rules for JavaScript's automatic semicolon insertion (ASI)?](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi). – pishpish Nov 02 '18 at 15:55
  • 1
    Even though Im not entirely sure about this, I think its better (and correct..?) to use `!==` instead of `!=`. Maybe that causes your problem :) – Petar Nov 02 '18 at 15:56
  • 1
    I've made a simplified version of your code here: http://jsfiddle.net/tw7p31dv, your code seems to be working. Can you check your files again – mxdi9i7 Nov 02 '18 at 15:57
  • 1
    `break` definitely ends the `for..of` loop, are you re-running the loop later? With the current code you should get only a single error, no matter how many filenames having a different extension. – Teemu Nov 02 '18 at 16:00
  • How is `files` value collected? – raina77ow Nov 02 '18 at 16:02
  • In response - Teemu, I am not running the loop again later. Petar - I have tried with both and still receive the issue. raina77ow - I am using fs.readdir – Luke Nov 02 '18 at 16:03
  • If you run mxdi9i7's fiddle (or [mine](https://jsfiddle.net/gvswt7x0/)), you can see only a single error message, and `break` works as expected. – Teemu Nov 02 '18 at 16:05
  • Please add the reading code. Do you see 9 '.txt' and one '.test' entries in the log? – raina77ow Nov 02 '18 at 16:08

1 Answers1

-1

I replicated your code, and it seems to work fine: https://repl.it/repls/ExcellentVapidApplicationframework

Maybe an issue caused by ASI, as Andreas suggested?

Community
  • 1
  • 1
misterbastean
  • 683
  • 5
  • 6