0

In the following script few things won't work right and i'm having a hard time trying to figure the why, First it gives a 'Null' value to the variable (The .txt file is not empty), second it returns to me that some funcitons are not expected at that moment (Such as'goto' was not expected) so it maybe something with the syntax?

@echo off
set name1=sample1
set name2=sample2
set name3=sample3
set ccount=0
del mes.txt
:WFC
ping localhost -n 2 >nul
if exist mes.txt (
SetLocal EnableDelayedExpansion
set /p cname=<mes.txt
set /a ccount=%ccount%+1
if %cname%==!name%ccount%! goto AllowedList
if %ccount%==20 goto Crasher
goto WFC

I tryed calling echo %cname% in the end of the script and appareantly the var have a "nil" value, plus I also turned the @echo on and haven't found nothing useful to understand the problem. The weirdest part is that if I take the variable declaration out of the 'if exist statemant', the whole script it works marvelously as in:

@echo off
set name1=sample1
set name2=sample2
set name3=sample3
set ccount=0
del mes.txt
:WFC
ping localhost -n 2 >nul
set /p cname=<mes.txt
if exist mes.txt (
SetLocal EnableDelayedExpansion
set /a ccount=%ccount%+1
if %cname%==!name%ccount%! goto AllowedList
if %ccount%==20 goto Crasher
goto WFC

So, this could be satisfying enough but it's not, there's a second software updating the mes.txt value all the time and I'm assuming that there will be some(not much, but some) cases in the second script where the var cname will get null value and still it will compare a null value with name1, completing excluding sample1 of the allowed lists. Can anyone solve this problem or at least explain the why?

Mitrek
  • 1,222
  • 8
  • 10
  • 1
    Your problem is delayed expansion (the lack of it) when reading `%cname%` and `%ccount%` (read [here](http://stackoverflow.com/a/30177832/2861476)) – MC ND Oct 08 '16 at 16:40

2 Answers2

0

Thankfully to the user MC ND, who awnsered me in the comments, I've managed to solve it, turns out that it was a really simple thing, his awnser was:

Your problem is delayed expansion (the lack of it) when reading %cname% and %ccount% (read here) – MC ND 24 mins ago

What I did to the script to make it work was:

@echo off
set name1=sample1
set name2=sample2
set name3=sample3
set ccount=0
del mes.txt
:WFC
ping localhost -n 2 >nul
set /p cname=<mes.txt
if exist mes.txt (
SetLocal EnableDelayedExpansion
set /a ccount=%ccount%+1
if !cname!==!name%ccount%! goto AllowedList
if !ccount!==20 goto Crasher
goto WFC
Community
  • 1
  • 1
Mitrek
  • 1,222
  • 8
  • 10
  • That cannot work as it doesn't even have balanced parentheses! – Compo Oct 08 '16 at 18:48
  • ...apart from the fact that it's opening a new `setlocal` on every iteration, using the local `ccount` within the loop and simply fails-to-fail because `cmd` permits more than 20 simultaneous open `setlocal` shells. – Magoo Oct 09 '16 at 02:27
0

My guess is that it should be something more like this

:WFC
Timeout 1 1>Nul
If Not Exist mes.txt GoTo :WFC
SetLocal EnableDelayedExpansion
Set/P cname=<mes.txt
Set/A ccount+=1
If %ccount% Equ 20 GoTo :Crasher
If "!name%ccount%!" Equ "%cname%" GoTo :AllowedList
GoTo :WFC
Compo
  • 36,585
  • 5
  • 27
  • 39