Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RENAME FILES IN A SEQUENCE

177 views
Skip to first unread message

JPS

unread,
Aug 19, 2008, 9:13:32 PM8/19/08
to
I am attempting to rename a large amount of files using a sequence,
ie,...I want the files renamed to FILE1.DOC, FILE2.DOC, etc.... any
way I can rename about 400 files automatically in a sequence as
described.

Thanks in advance

001.d...@gmail.com

unread,
Aug 20, 2008, 12:39:25 AM8/20/08
to

@echo off
for %%i in (*.doc) do set /a count+=1 && call:ren "%%i"
goto:eof

:ren
set num=00%count%
set num=%num:~-3%
ren %1 "file%num%.doc"

Timo Salmi

unread,
Aug 20, 2008, 1:37:37 AM8/20/08
to

12} How can I rename all my .JPG files sequentially in a folder?
http://www.netikka.net/tsneti/info/tscmd012.htm

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Tom Lavedas

unread,
Aug 20, 2008, 8:36:08 AM8/20/08
to
On Aug 20, 12:39 am, 001.dmi...@gmail.com wrote:

That approach is a bit problematic. Such renaming can get confused
because the new names can get 'reprocessed' by the FOR loop.

It is better to create a list and then process the list ...

@echo off
for /f "delims=" %%i in ('dir *.doc /b') do call :ren "%%i"
goto:eof

:ren
set /a count+=1 & set num=00%count%
ren %1 "file%%num:~-3%%.doc"

Another way is to hide the renamed files ...

@echo off
for %%i in (*.doc) do call :ren "%%i"
attrib -h *.doc
goto:eof

:ren
set /a count+=1 & set num=00%count%
ren %1 "file%%num:~-3%%.doc"
attrib +h "file%%num:~-3%%.doc"

The first approach can allow the files to be sorted before renaming,
such as by name or date. Otherwise, they are renamed in a fairly
arbitrary order (based on their entry location in the file system's
naming table, I believe).

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

001.d...@gmail.com

unread,
Aug 20, 2008, 11:00:45 AM8/20/08
to
> because the new names can get 'reprocessed' by the FOR loop.

Just set %count% to "0" and `setlocal` for case of repeat of batch in
one session.

@echo off
setlocal
set count=0


for %%i in (*.doc) do set /a count+=1 && call:ren "%%i"
goto:eof

:ren
set num=00%count%
ren %1 "file%num~-3%.doc"

Tom Lavedas

unread,
Aug 20, 2008, 11:41:31 AM8/20/08
to

I don't see how creating a child environment space fixes a problem
with the file system processing. It is a proven fact that the wild
card search can create a new name, say in renaming of OldnameX.doc to
File001.doc, which can be 'reprocessed' by the still running FOR loop
and become a name conversion from File001.doc to File001075.doc, say.
And then to File001075099.doc, ad infinitum. That was the problem I
was addressing.

foxidrive

unread,
Aug 20, 2008, 11:52:24 AM8/20/08
to
On Wed, 20 Aug 2008 08:41:31 -0700 (PDT), Tom Lavedas <tglb...@cox.net>
wrote:

>On Aug 20, 11:00 am, 001.dmi...@gmail.com wrote:
>> > because the new names can get 'reprocessed' by the FOR loop.
>>
>> Just set %count% to "0" and `setlocal` for case of repeat of batch in
>> one session.
>>
>> @echo off
>> setlocal
>> set count=0
>> for %%i in (*.doc) do set /a count+=1 && call:ren "%%i"
>> goto:eof
>>
>> :ren
>> set num=00%count%
>> ren %1 "file%num~-3%.doc"
>
>I don't see how creating a child environment space fixes a problem
>with the file system processing. It is a proven fact that the wild
>card search can create a new name, say in renaming of OldnameX.doc to
>File001.doc, which can be 'reprocessed' by the still running FOR loop
>and become a name conversion from File001.doc to File001075.doc, say.
>And then to File001075099.doc, ad infinitum. That was the problem I
>was addressing.

Tom's right in both his comments, including the "for /f" command which
eliminates this issue.

This code will show the problem in both NTFS and FAT32, though NTFS was not
as severely affected.

@echo off
echo 1bc>1bc.txt
echo 2bc>2bc.txt
echo 3bc>3bc.txt
echo 4bc>4bc.txt
echo 5bc>5bc.txt
for %%a in (*.txt) do copy "%%a" "a%%a"&echo "%%a" >>file.lst


001.d...@gmail.com

unread,
Aug 20, 2008, 1:39:37 PM8/20/08
to
>То Tom Lavedas

> I don't see how creating a child environment space fixes a problem
> with the file system processing. It is a proven fact that the wild
> card search can create a new name, say in renaming of OldnameX.doc to
> File001.doc, which can be 'reprocessed' by the still running FOR loop
> and become a name conversion from File001.doc to File001075.doc, say.
> And then to File001075099.doc, ad infinitum. That was the problem I
> was addressing.

Im sorry, I don't understand the problem.
I tested my batch and it worked fine. What did I do wrong? I think,
FOR makes a list of files for first and after it makes its job.

PS. Win XP HE SP3, ntfs

Todd Vargo

unread,
Aug 20, 2008, 5:49:01 PM8/20/08
to
<001.d...@gmail.com> wrote:
> >?? Tom Lavedas

This is an old problem explained several times over the years. The jist of
the problem is, when a file is renamed, the new name is created in the next
available slot in the directory before the old name is removed. To the FOR
command, when the new entry fits the same mask (*.doc), and the new entry is
created after the current pointer, it considers it as not yet processed and
processes it again. This issue can be avoided by renaming the files so they
do not match the same mask (in this case, use a different extension).

Note, this anomaly usually only affects a single file in 8.3 file systems.
In systems where LFNs are present, it can occur multiple times depending on
the number of characters being added to the filename. To see an example type
the following at command prompt.

md test
cd test
echo.>first.doc
echo.>second.doc
echo.>third.doc
for %f in (*.doc) do ren %f 00%f
dir /b *.doc

==== Windows 98 Screen Capture ====
E:\>md test

E:\>cd test

E:\test>echo.>first.doc

E:\test>echo.>second.doc

E:\test>echo.>third.doc

E:\test>for %f in (*.doc) do ren %f 00%f

E:\test>ren FIRST.DOC 00FIRST.DOC

E:\test>ren SECOND.DOC 00SECOND.DOC

E:\test>ren THIRD.DOC 00THIRD.DOC

E:\test>ren 00FIRST.DOC 0000FIRST.DOC

E:\test>dir /b *.doc
00SECOND.DOC
00THIRD.DOC
0000FIRST.DOC

==== End Capture ====

As you can see from the output, first.doc was renamed twice and it was only
an 8.3 name after the first rename. Now consider renaming 400 files with
this method, adding several characters into the new names. Many more than a
single file will be renamed multiple times., and some will be renamed more
than twice. Creating a rename list or changing something to break the mask
will prevent this. In this case, just changing the extension is enough to
prevent reprocessing the same files. For a large number of file, creating a
list is the more efficient method.

Remove the previous test directory and then try this example at the prompt.

md test
cd e:\test
echo.>first.doc
echo.>second.doc
echo.>third.doc
for %f in (*.doc) do ren %f "00%~nf.tmpdoc
ren *.tmpdoc *.doc
dir /b *.doc

==== Win95cmd Screen Capture ====
E:\>md test

E:\>cd e:\test

E:\test>echo.>first.doc

E:\test>echo.>second.doc

E:\test>echo.>third.doc

E:\test>for %f in (*.doc) do ren %f "00%~nf.tmpdoc

E:\test>ren first.doc "00first.tmpdoc

E:\test>ren second.doc "00second.tmpdoc

E:\test>ren third.doc "00third.tmpdoc

E:\test>ren *.tmpdoc *.doc

E:\test>dir /b *.doc
00first.doc
00third.doc
00second.doc

==== End Capture ====

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Dr J R Stockton

unread,
Aug 21, 2008, 12:37:01 PM8/21/08
to
In alt.msdos.batch.nt message <jj0rk.35055$co7....@nlpi066.nbdc.sbc.co
m>, Wed, 20 Aug 2008 17:49:01, Todd Vargo <tlv...@sbcglobal.netz>
posted:

>
>This is an old problem explained several times over the years. The jist of
>the problem is, when a file is renamed, the new name is created in the next
>available slot in the directory before the old name is removed. To the FOR
>command, when the new entry fits the same mask (*.doc), and the new entry is
>created after the current pointer, it considers it as not yet processed and
>processes it again. This issue can be avoided by renaming the files so they
>do not match the same mask (in this case, use a different extension).

Usually, the nature of the new and old names is such that, if one uses
the minimum wildness of wild-cards, there will be no problem.

if that is not certain, the files can be MOVEd to a temporary directory
and MOVEd back, with at least one set of MOVE being a renaming one. On
a sensible OS, MOVE will only alter directory entries, so should be
quick. If the second MOVE uses *.*, its directory changes should cache
well.

If the new names include a clash with old names that are to be
unchanged, there's inevitably a problem, however it is done. One can
test with NOT EXIST, and move the problem cases elsewhere for further
attention.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Todd Vargo

unread,
Aug 21, 2008, 5:38:30 PM8/21/08
to
Dr J R Stockton wrote:
> Todd Vargo posted:

> >
> >This is an old problem explained several times over the years. The jist
of
> >the problem is, when a file is renamed, the new name is created in the
next
> >available slot in the directory before the old name is removed. To the
FOR
> >command, when the new entry fits the same mask (*.doc), and the new entry
is
> >created after the current pointer, it considers it as not yet processed
and
> >processes it again. This issue can be avoided by renaming the files so
they
> >do not match the same mask (in this case, use a different extension).
>
> Usually, the nature of the new and old names is such that, if one uses
> the minimum wildness of wild-cards, there will be no problem.
>
> if that is not certain, the files can be MOVEd to a temporary directory
> and MOVEd back, with at least one set of MOVE being a renaming one. On
> a sensible OS, MOVE will only alter directory entries, so should be
> quick. If the second MOVE uses *.*, its directory changes should cache
> well.
>
> If the new names include a clash with old names that are to be
> unchanged, there's inevitably a problem, however it is done. One can
> test with NOT EXIST, and move the problem cases elsewhere for further
> attention.

I like using MOVE too but...

What does "On a sensible OS" mean?

Does REN not cache as well as MOVE?

Dr J R Stockton

unread,
Aug 22, 2008, 7:49:32 AM8/22/08
to
In alt.msdos.batch.nt message <pflrk.37300$ZE5....@nlpi061.nbdc.sbc.com
>, Thu, 21 Aug 2008 17:38:30, Todd Vargo <tlv...@sbcglobal.netz>
posted:

>
>I like using MOVE too but...
>
>What does "On a sensible OS" mean?

Are there no EFL courses in your country?

>Does REN not cache as well as MOVE?

No doubt it also caches. But with the MOVE method doing two actions per
file, the cacheing could be more beneficial for MOVE. In particular,
with MOVE dir\*.* dest the system should be aware that all files are
to be moved, and might take advantage of that.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Todd Vargo

unread,
Aug 22, 2008, 5:05:07 PM8/22/08
to

"Dr J R Stockton" <j...@merlyn.demon.co.uk> wrote in message
news:fS0eV3AM...@invalid.uk.co.demon.merlyn.invalid...

> In alt.msdos.batch.nt message <pflrk.37300$ZE5....@nlpi061.nbdc.sbc.com
> >, Thu, 21 Aug 2008 17:38:30, Todd Vargo <tlv...@sbcglobal.netz>
> posted:
> >
> >I like using MOVE too but...
> >
> >What does "On a sensible OS" mean?
>
> Are there no EFL courses in your country?

You made the comment so I'm curious why you made it. A sensible person would
have just given an honest answer.

mik3...@gmail.com

unread,
Aug 23, 2008, 9:40:02 AM8/23/08
to

if you can use vbscript on your system. Save the below code as
script.vbs and on command line type: cscript script.vbs


Set FSO = CreateObject("Scripting.FileSystemObject")
sDir = "C:\temp"
Set objFolder = FSO.GetFolder(sDir)
counter = 1
For Each Files In objFolder.Files
If FSO.GetExtensionName(Files) = "doc" Then
NewFileName = Replace(Files.Name,".doc", Padding(3,counter) &
".doc")
Files.Name = NewFileName
counter=counter+1
End If
Next

Function Padding(ValueToPad, TheDigits)
theLen = Len(TheDigits)
If theLen < ValueToPad Then
Padding = String(ValueToPad-theLen,"0") & TheDigits
Else
Padding = TheDigits
End If
End Function

Timo Salmi

unread,
Aug 23, 2008, 6:40:08 PM8/23/08
to
mik3...@gmail.com wrote:
> On Aug 20, 9:13 am, JPS <STEPANSK...@gmail.com> wrote:
>> I am attempting to rename a large amount of files using a sequence,
>> ie,...I want the files renamed to FILE1.DOC, FILE2.DOC, etc.... any
>> way I can rename about 400 files automatically in a sequence as
>> described.

> if you can use vbscript on your system. Save the below code as


> script.vbs and on command line type: cscript script.vbs

Nice! Written in the spirit of the newsgroup's topic as a batch driven
VBS we can have (I have it for .JPG instead of .DOC)

@echo off & setlocal enableextensions
::
:: Set the source folder and check that it contains .JPG files
set SourceDir=C:\_M
if not exist "%SourceDir%\*.JPG" (
echo No .JPG files found in "%SourceDir%"
goto :EOF)
::
:: Build a Visual Basic Script
set skip=
set temp_=%temp%
if exist "%mytemp%\" set temp_=%mytemp%
set vbs_=%temp_%\tmp$$$.vbs
>"%vbs_%" findstr "'%skip%VBS" "%~f0"
::
:: Run the script with Microsoft Windows Script Host Version 5.6
cscript //nologo "%vbs_%" "%SourceDir%"
::
:: Clean up
for %%f in ("%vbs_%") do (
if exist %%f del %%f)
endlocal & goto :EOF
'
'The Visual Basic Script
'
Set arg = WScript.Arguments 'VBS
Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
Set folderspec = fso.GetFolder(arg(0)) 'VBS
Set f = fso.GetFolder(folderspec) 'VBS
Set fc = f.Files 'VBS
Set i=0
For Each f1 in fc 'VBS
extension = fso.GetExtensionName(f1) 'VBS
If UCase(extension) = "JPG" Then 'VBS
i = i + 1 'VBS
s = "echo @ren " 'VBS
s = s & f1 & " " 'VBS
basename = fso.GetBaseName(f1) 'VBS
s = s & basename 'VBS
s = s & PadFn(CStr(i),4) 'VBS
s = s & "." & extension 'VBS
Wscript.Echo s 'VBS
End If 'VBS
Next 'VBS
'
Function PadFn (str, n) 'VBS
PadFn = Right(String(n-1,"0")&str,n) 'VBS
End Function 'VBS

The (safety) output might be e.g. the following which could be
redirected to a new batch file which then can be run after one is
convinced that it is exactly what one wants.

echo @ren C:\_M\ABSORB.JPG ABSORB0001.JPG
echo @ren C:\_M\bikemir.jpg bikemir0002.jpg
echo @ren C:\_M\CATEYE.JPG CATEYE0003.JPG
echo @ren C:\_M\CRESBAG.JPG CRESBAG0004.JPG
echo @ren C:\_M\PEUGBAG.JPG PEUGBAG0005.JPG
echo @ren C:\_M\PEUGEOT.JPG PEUGEOT0006.JPG
echo @ren C:\_M\PRPUMP.JPG PRPUMP0007.JPG
echo @ren C:\_M\SKATES.JPG SKATES0008.JPG
echo @ren C:\_M\SKIEQUIP.JPG SKIEQUIP0009.JPG
echo @ren C:\_M\SONYRAD.JPG SONYRAD0010.JPG
echo @ren C:\_M\TACX.JPG TACX0011.JPG
echo @ren C:\_M\WELLGO.JPG WELLGO0012.JPG

Todd Vargo

unread,
Aug 23, 2008, 9:31:09 PM8/23/08
to
Timo Salmi wrote:
> mik3...@gmail.com wrote:
> > On Aug 20, 9:13 am, JPS <STEPANSK...@gmail.com> wrote:
> >> I am attempting to rename a large amount of files using a sequence,
> >> ie,...I want the files renamed to FILE1.DOC, FILE2.DOC, etc.... any
> >> way I can rename about 400 files automatically in a sequence as
> >> described.
>
> > if you can use vbscript on your system. Save the below code as
> > script.vbs and on command line type: cscript script.vbs
>
> Nice! Written in the spirit of the newsgroup's topic as a batch driven
> VBS we can have (I have it for .JPG instead of .DOC)
>
snip...

>
> The (safety) output might be e.g. the following which could be
> redirected to a new batch file which then can be run after one is
> convinced that it is exactly what one wants.
>
> echo @ren C:\_M\ABSORB.JPG ABSORB0001.JPG
> echo @ren C:\_M\bikemir.jpg bikemir0002.jpg
> echo @ren C:\_M\CATEYE.JPG CATEYE0003.JPG
> echo @ren C:\_M\CRESBAG.JPG CRESBAG0004.JPG
> echo @ren C:\_M\PEUGBAG.JPG PEUGBAG0005.JPG
> echo @ren C:\_M\PEUGEOT.JPG PEUGEOT0006.JPG
> echo @ren C:\_M\PRPUMP.JPG PRPUMP0007.JPG
> echo @ren C:\_M\SKATES.JPG SKATES0008.JPG
> echo @ren C:\_M\SKIEQUIP.JPG SKIEQUIP0009.JPG
> echo @ren C:\_M\SONYRAD.JPG SONYRAD0010.JPG
> echo @ren C:\_M\TACX.JPG TACX0011.JPG
> echo @ren C:\_M\WELLGO.JPG WELLGO0012.JPG
>
> All the best, Timo

Nice? I read OP's request as, "as described", the basename of all files to
be renamed with the same prefix in front of the incremented number. i.e.
FILE :-(

Timo Salmi

unread,
Aug 24, 2008, 1:24:06 AM8/24/08
to
Todd Vargo <tlv...@sbcglobal.netz> wrote:
> Timo Salmi wrote:
>> mik3...@gmail.com wrote:
>>> On Aug 20, 9:13 am, JPS <STEPANSK...@gmail.com> wrote:
>>>> I am attempting to rename a large amount of files using a sequence,
>>>> ie,...I want the files renamed to FILE1.DOC, FILE2.DOC, etc.... any
>>>> way I can rename about 400 files automatically in a sequence as
>>>> described.

>> The (safety) output might be e.g. the following which could be


>> redirected to a new batch file which then can be run after one is
>> convinced that it is exactly what one wants.
>>
>> echo @ren C:\_M\ABSORB.JPG ABSORB0001.JPG
>> echo @ren C:\_M\bikemir.jpg bikemir0002.jpg
>> echo @ren C:\_M\CATEYE.JPG CATEYE0003.JPG

> Nice? I read OP's request as, "as described", the basename of all files to


> be renamed with the same prefix in front of the incremented number. i.e.
> FILE :-(

Thanks for pointing that out. The change required the achieve that,
however, is fortunately totally trivial. Actually, more importantly, the
code VBS-aided I posted is now slightly honed in other respects at

12} How can I rename all my .JPG files sequentially in a folder?

http://www.netikka.net/tsneti/info/tscmd012.htm#vbs

In the case of the OP's format of request the only, simple change
required (besides the JPG to DOC) is changing
basename = fso.GetBaseName(f1) 'VBS
to
basename = "FILE" 'VBS

The output then starts with
rem @ren "C:\_M\ABSORB.JPG" "FILE0001.JPG"
rem @ren "C:\_M\bikemir.jpg" "FILE0002.jpg"
rem @ren "C:\_M\CATEYE.JPG" "FILE0003.JPG"

Todd Vargo

unread,
Aug 25, 2008, 12:02:46 AM8/25/08
to
Timo Salmi wrote:
> Thanks for pointing that out. The change required the achieve that,
> however, is fortunately totally trivial. Actually, more importantly, the
> code VBS-aided I posted is now slightly honed in other respects at
>
> 12} How can I rename all my .JPG files sequentially in a folder?
> http://www.netikka.net/tsneti/info/tscmd012.htm#vbs
>
> In the case of the OP's format of request the only, simple change
> required (besides the JPG to DOC) is changing
> basename = fso.GetBaseName(f1) 'VBS
> to
> basename = "FILE" 'VBS
>
> The output then starts with
> rem @ren "C:\_M\ABSORB.JPG" "FILE0001.JPG"
> rem @ren "C:\_M\bikemir.jpg" "FILE0002.jpg"
> rem @ren "C:\_M\CATEYE.JPG" "FILE0003.JPG"
>
> All the best, Timo

Excellent, and quoting was included this time as well.

0 new messages