tags:

views:

1580

answers:

3

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.

+1  A: 

You may need to use delayed environment variable expansion. See CMD /? for more help about this:

/V:ON Enable delayed environment variable expansion using ! as the delimiter. For example, /V:ON would allow !var! to expand the variable var at execution time. The var syntax expands variables at input time, which is quite a different thing when inside of a FOR loop.

...

Delayed environment variable expansion is NOT enabled by default. You can enable or disable delayed environment variable expansion for a particular invocation of CMD.EXE with the /V:ON or /V:OFF switch. You can enable or disable delayed expansion for all invocations of CMD.EXE on a machine and/or user logon session by setting either or both of the following REG_DWORD values in the registry using REGEDIT.EXE:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion

and/or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion

to either 0x1 or 0x0. The user specific setting takes precedence over the machine setting. The command line switches take precedence over the registry settings.

If delayed environment variable expansion is enabled, then the exclamation character can be used to substitute the value of an environment variable at execution time.

Lucero
A: 

Lucero is right.

Example code:

@echo off
setlocal enabledelayedexpansion
echo.
set numLines=0
echo examining file '%~f0'
echo.
rem loop N times, once for each line in the file
for /f %%a in (%~f0) do (

    rem add 1 to the numLines variable
    set /a numLines=!numLines!+1

    rem echo the first symbol from the line 
    echo line !numLines!: %%a
)

rem Display the number of lines in the file
echo.
echo The file '%~f0' has %numLines% lines.
echo.

pause
echo.
endlocal
Cheeso
A: 

Just wanted to add that the key to utilizing delayed expansion is referencing the variable using the !var! syntax versus %var%. Best I can tell in XP delayed expansion is enabled by default, you just have to reference the var using !. That fixed my loop. Found this in the help mentioned by the other posters:

Enable delayed environment variable expansion using ! as the delimiter. For example, /V:ON would allow !var! to expand the variable var at execution time. The var syntax expands variables at input time, which is quite a different thing when inside of a FOR loop.

I overlooked the !! bit at first.

Kyle