1

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
  • This question is basically about the same problem. Commented Jul 13, 2011 at 19:16

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

&& implies checking the result of executing the previous command, & doesn't. Also it's EnableDelayedExpansion (EnableExtensions is about something different).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.