I'm trying to write a batch that renames all
current files in sequential order. The issue that
i'm having is that the current batch script
doesn't go to the next file in the folder and
rename it by an increment of one. It seems to
be doing the following:sample file name: testing$2342.xml,
testing$2346.xml,testing$2348.xml, etcrenamed file name: testing$4.xml,
testing$6.xml, etcdesired result: testing$1.xml, testing$2.xml,
testing$3.xmlcode:
SET SRCDIR=c:\TEST
set num=1
set gmcode=
:start
For /f "tokens=1-2,3 delims=$." %%a in ('dir /b
%XDIR%\*.xml') do (rename %XDIR%\*.xml
%%a$%num%.xmlset /a num+=1
rem timeout.exe 1
goto start
)
Take the GOTO START
out because it will loop at the close paran.
Also you rename command is messed up...
rem SET SRCDIR=c:\TEST rem shouldn't it be like this... set XDIR=c:\TEST set /a num=1 set gmcode= For /f %%a in ('dir /b %XDIR%\*.xml') do ( rename %%a %%a$%num%.xml set /a num+=1 )
If i remove the GOTO START it only renames one file. The batch
job never exits out of the loop and keeping saying "a duplicate
file name exits or file name cannot be found". And i think that's
because without the GOTO START the num variable never gets
incremented by 1.
(moved from other double-post)
you need enabledel or call-out to sub. enab is easier:
setlocal enabledelayedexpansion
SET SRCDIR=c:\TEST
set num=1
:: you don't need this as the for-loop loops without it.
:start
For /f "tokens=1-2,3 delims=$." %%a in ('dir /b
%XDIR%\*.xml') do (
:: note the replacement of percents with exclms in foll.
rename %XDIR%\*.xml
%%a$!num!.xml
set /a num+=1
:: removed the goto, not needed or wanted here
rem timeout.exe 1
)
Sorry for the double post...let's use this thread as the main
one then...Anyways...
Thanks nbrane, however, the logic in your batch does the
same thing in the rename of the file as my original batch...it
renames them improperly.
The file names are being renamed as:
TEST$2.XML - TEST$9, and then TEST$19.XML-
TEST$91.XML
So for whatever reason the first file is starting with 2 and also
there's a big gap between the numbering of the files. Any
thoughts?
sample file name: testing$2342.xml,
testing$2346.xml,testing$2348.xml, etcmaybe best to back up and punt. I'll just start from scratch
with this attempt:
setlocal enabledelayedexpansion
SET SRCDIR=c:\TEST
set num=0
pushd %srcdir%
For /f "tokens=1-2 delims=$." %%a in ('dir /b /o:n *.xml') do (
set /a num+=1
echo rename %%a$%%b.xml %%a$!num!.xml
rename %%a$%%b.xml %%a$!num!.xml
)
::------- end
i suspect the wildcard was making a hash of things in the
original code, also some other parts i didna unerstan
(pretty much the same thing as ace had in
resp #2 above, exept for adding the !!)
Hi
NBane code should work fine.
I'm 1 right in thinking that there are some files already numbered in your directory, if so they would be renamed and renumberd ie.
Testig1.xml might be renumbered as Testing19.xml or another
number depending on the value Num holds.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |