CMD For Loop does not hold set /a value

Posted by Bob Hope on Stack Overflow See other posts from Stack Overflow or by Bob Hope
Published on 2009-06-11T12:07:23Z Indexed on 2010/04/15 20:53 UTC
Read the original article Hit count: 199

Filed under:
|
|

Didn't know how to explain this well, so here is the code

@echo off
set test=0

for /f %%a in (textfile.txt) do (
rem loops five times(5 lines in textfile.txt)

set /a test=test+1
rem Adds 1 to Test

echo %%a
rem Echo's correct line in file

echo %test%
rem Echo's whatever X was before the loop

)

echo %test%
rem Displays the correct value of X

pause

this is just an example of where I am finding the problem, txtfile.txt has 5 lines, so the for loop goes 5 times, each time, test gets +1 to it, and the /a displays the correct value of X, but the last echo on the for loop displays 0, which is what test is set to before the loop.

The value of test is not changing until the loop is finished... is there any way to fix this?

Here is what I want to accomplish, as there may be an easier way: I want to run a for loop that findstr's all rtf's in a series of sub folders, and copies them all to a all directory with a new name, 1.rtf,2.rtf,3.rtf, etc. The reason I need to rename them on the transfer is they all have the same name.

© Stack Overflow or respective owner

Related posts about cmd

Related posts about for-loop