Thanks in advance
@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"
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
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/
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.
>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
> 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
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)
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.
I like using MOVE too but...
What does "On a sensible OS" mean?
Does REN not cache as well as MOVE?
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)
You made the comment so I'm curious why you made it. A sensible person would
have just given an honest answer.
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
> 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
>
> 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 :-(
>> 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"
Excellent, and quoting was included this time as well.