How to loop to next file in batch fileb

Hewlett-packard Pavilion a4316f-b deskto...
May 11, 2010 at 10:13:56
Specs: Windows 7
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, etc

renamed file name: testing$4.xml,
testing$6.xml, etc

desired result: testing$1.xml, testing$2.xml,
testing$3.xml

code:
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%.xml

set /a num+=1
rem timeout.exe 1
goto start
)


See More: How to loop to next file in batch fileb

Report •


#1
May 11, 2010 at 10:41:39
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
  )


Report •

#2
May 11, 2010 at 11:03:32
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.

Report •

#3
May 11, 2010 at 12:21:52
oops, someone double-posted here!
see #1 at:
http://www.computing.net/answers/pr...

Report •

Related Solutions

#4
May 11, 2010 at 13:38:38
(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
)


Report •

#5
May 11, 2010 at 13:40:52
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?


Report •

#6
May 11, 2010 at 13:51:11
sample file name: testing$2342.xml,
testing$2346.xml,testing$2348.xml, etc

maybe 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 !!)


Report •

#7
May 12, 2010 at 13:46:09
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.

Report •


Ask Question