问题
Possible Duplicate:
Random variable not changing in “for” loop in windows batch file
I have text file with list of files I want to rename. This is my code:
for /f %%i in (tmp.txt) do set script_name=%%i & ren %script_name% %script_name:~0,9%%num%%script_name:~15,9%.sql
But in second part it takes %script_name%
only in the first iteration of the loop. So
ren %script_name% %script_name:~0,9%%num%%script_name:~15,9%.sql
always do the same.
Please help to fix that.
回答1:
Look at set /?
and then explore delayed environment expansion. You would first
SETLOCAL ENABLEDELAYEDEXPANSION
(look at setlocal /?
as well) and then wrap variables in ren
with !'s rather than %'s. I would also use && concatenating the commands, so the first one completes before the second one starts.
来源:https://stackoverflow.com/questions/6680409/for-loop-in-batch-file