0
votes

This is my code:

@echo off
setlocal enabledelayedexpansion
echo,&set/p x=Step1.Please input type of files(type * for all files)
echo,&set/p sign=Step2.Please input prefix
for %%i in (*.%x%) do (
set "filename=%%i"
if defined sign set "filename=%sign%!filename!"
if not "%%i"=="!filename!" ren "%%i" "!filename!"
)
echo,Works done!press any key to quit...
pause>nul

1
Please, see hereMC ND
@MCND thanks, but... is there any way to solve this problem?Though I used (??.%x%) to void it in my case.Chance Door
See answer. Not more detailed than the original link, but maybe one solution to your case.MC ND

1 Answers

3
votes

In short, the list of files retrieved by the for command is dynamic. If initially there is more than one file matching the set, then the list will be rereaded (more info here) and if the first processed file is renamed to anything that also matches the set in for command, it will be reprocessed.

One solution is to first retrieve the full list and then process it. It can be done with for /f and dir commands

for /f "delims=" %%i in ('dir /b /a-d "*.%x%"') do ( ....

for /f will always retrieve all the information to process before starting to process it. So, the dir command is executed once and then its output processed.

Other solution (as indicated in comments by OP) is to ensure the indicated set of files in for command will not match the new renamed files.