for loop in batch file [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-24 08:25:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!