57

I'm trying to read text lines from a file, and increment a counter so I can eventually simulate an array in DOS.

I'd like to be able to store the lines of text in a DOS array for further processing.

My current attempt is:

set TEXT_T="myfile.txt"

set /a c=1

FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo %%i,  %c%
)

But the variable c is not incrementing; it stays at 1.

Suggestions welcome.

Thanks, Mike

Mike
  • 593
  • 1
  • 5
  • 5

5 Answers5

101

The problem with your code snippet is the way variables are expanded. Variable expansion is usually done when a statement is first read. In your case the whole FOR loop and its block is read and all variables, except the loop variables are expanded to their current value.

This means %c% in your echo %%i, %c% expanded instantly and so is actually used as echo %%i, 1 in each loop iteration.

So what you need is the delayed variable expansion. Find some good explanation about it here.

Variables that should be delay expanded are referenced with !VARIABLE! instead of %VARIABLE%. But you need to activate this feature with setlocal ENABLEDELAYEDEXPANSION and reset it with a matching endlocal.

Your modified code would look something like that:

set TEXT_T="myfile.txt"

set /a c=1

setlocal ENABLEDELAYEDEXPANSION

FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
  set /a c=c+1

  echo %%i, !c!
)

endlocal
Frank Bollack
  • 24,478
  • 5
  • 49
  • 58
  • Super Duper! This is just what I needed. I modified the script accordingly and it now runs as I expected. Thanks for the quick answer. -- Mike – Mike May 27 '10 at 18:11
  • @Frank Bollack. thanks so much for this explanation, I learned something new. I used this technique to answer this other question: http://serverfault.com/questions/664202/batch-file-that-kills-a-certain-process . It works, but I'm not sure how I should reference the variables _outside_ the FOR loop. Does it make a difference, using !c! or %c%, in that IF I have after the FOR? – pgr Feb 02 '15 at 10:52
  • 1
    I don't understand why this answer is on the bottom - it's the only one that works and including the explanation of why is key! Thank you! This should be the accepted answer – Thronk Feb 19 '15 at 16:51
  • `!c!` was the problem -- I wasn't using it! *sigh* Thank you! – AndrewRalon Jul 20 '17 at 15:18
7

I would like to add that in case in you create local variables within the loop, they need to be expanded using the bang(!) notation as well. Extending the example at https://stackoverflow.com/a/2919699 above, if we want to create counter-based output filenames

set TEXT_T="myfile.txt"

set /a c=1

setlocal ENABLEDELAYEDEXPANSION

FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
    set /a c=c+1
    set OUTPUT_FILE_NAME=output_!c!.txt
    echo Output file is !OUTPUT_FILE_NAME!
    echo %%i, !c!
)

endlocal
Community
  • 1
  • 1
curryage
  • 481
  • 1
  • 6
  • 19
5

Or you can do this without using Delay.

set /a "counter=0"

-> your for loop here

do (
   statement1
   statement2
   call :increaseby1
 )

:increaseby1
set /a "counter+=1"
Kent Pawar
  • 2,378
  • 2
  • 29
  • 42
0
set TEXT_T="myfile.txt"
set /a c=1

FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
    set /a c+=1
    set OUTPUT_FILE_NAME=output_%c%.txt
    echo Output file is %OUTPUT_FILE_NAME%
    echo %%i, %c%
)
0

What about this simple code, works for me and on Windows 7

set cntr=1
:begin
echo %cntr%
set /a cntr=%cntr%+1
if %cntr% EQU 1000 goto end
goto begin

:end