0

I am reading lines from a text file. I need to ignore the first half and after a certain line is reached, assign the lines read into a variable. The 2nd if statement never fulfills even though the output shows that q has incremented from 0 to 1.

Code:

set q=0

for /f "tokens=*" %%a in (myfilename) do (

   if "%%a" == "# My Trigger Text" ( 
     set /A "q+=1"
     set q    :: Debug Statement 1
     echo %%a
   )

   if %q% GTR 0 ( 
     set /A "q+=1"
     set q    :: Debug Statement 2
     echo %%a
     [Other code will eventually go here; nothing right now]
   )
   :: set q   :: Debug Statement 3
)
pause

Output with Debug Statement 3 commented out shows:

ECHO is off. 
q=1
# My Trigger Text
Press any key to continue...

If I uncomment Debug Statement 3, the output is:

ECHO is off. 
q=0
q=0
q=0
q=0
q=0
q=0
q=1
# My Trigger Text
q=1
q=1
q=1
q=1
q=1
q=1
q=1
Press any key to continue...

So q is getting incremented the first time when it hits the trigger text, but it is not evaluating q to be greater than 0 to get inside the functional loop.

JoeBlow
  • 61
  • 1
  • 2
  • 7
  • 1
    Check [this question](http://stackoverflow.com/questions/5615206/windows-batch-files-setting-variable-in-for-loop) with its answer. You need [delayed expansion](http://ss64.com/nt/delayedexpansion.html) for your `q` variable. – J.Baoby Jan 26 '17 at 17:38
  • Thank you! That worked perfectly. – JoeBlow Jan 26 '17 at 17:59
  • 1
    Possible duplicate of [windows batch files: setting variable in for loop](http://stackoverflow.com/questions/5615206/windows-batch-files-setting-variable-in-for-loop) – aschipfl Jan 26 '17 at 18:43

0 Answers0