Solved

dos batch rename file extension

Posted on 2006-11-19
11
375 Views
Last Modified: 2010-04-16
Hi There,

In a .cmd file I want to rename all file extensions in an incrementing way.
Like ren test.jpg test.00001, ren  test1.jpg test.00002 etc
I have less then 100.000 files, so it has to start with 5 digits

Erik
0
Comment
Question by:fmew
11 Comments
 
LVL 10

Expert Comment

by:fostejo
ID: 17974530
fmew,

Rather than a cmd file or script (which will, of neccessity, have very limited functionality), it may be worthwhile having a look at a generic file renaming tool like Batch File Renamer (http://www.1000files.com/Utilities/File_and_Disk_Management/Batch_File_Utility_8594_Review.html) - this allows you to play around with the renaming rules and preview the results prior to actually making any changes to the file names..

Obviously, it it *has* to be a script, let us know !

cheers,
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 17974547
for me could be easier to write an execitale for this task or use ACDSEE, XnView, Irfan view
here i found somedesciption
http://graphicssoft.about.com/cs/renamers/ht/renamexp.htm
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17974917
Hey fmew,

Try this:

@echo off
setlocal enabledelayedexpansion

set count=0

:: Dont run in same directory as this batch!!
cd Data

for %%a in (*.*) do (
      set OldName=%%a
      set OldName=!OldName:~0,-3!
      
      if !count! lss 100000 set NewName=!OldName!!count!
      if !count! lss 10000 set NewName=!OldName!0!count!
      if !count! lss 1000 set NewName=!OldName!00!count!
      if !count! lss 100 set NewName=!OldName!000!count!
      if !count! lss 10 set NewName=!OldName!0000!count!

      if not exist !NewName! ren %%a !NewName!

      set /a count=!count!+1
)

cd ..

endlocal
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17974963
I just re-read your question. I overlooked you not wanting to keep the file name. Try the following:

@echo off
setlocal enabledelayedexpansion

set count=0

cd Data

for %%a in (*.*) do (
      if !count! lss 100000 set NewName=test!count!
      if !count! lss 10000 set NewName=test0!count!
      if !count! lss 1000 set NewName=test00!count!
      if !count! lss 100 set NewName=test000!count!
      if !count! lss 10 set NewName=test0000!count!

      if not exist !NewName! ren %%a !NewName!

      set /a count=!count!+1
)

cd ..

endlocal
0
 

Author Comment

by:fmew
ID: 17978269
Great Raymun,

I just had to change test!count! to *.!count!  course I just need the extension to change.
What I want to do now is move and split the files i have (say *.00001 to *.20000) into different dirs with a maximum of say 1000 files in each dir.

I think now that maybe this can be done in once by just counting the files and move them to different dirs.

So (and I want to grade you dubble), do you know one way or the other to do this?

tnx Erik
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Accepted Solution

by:
Raymun earned 500 total points
ID: 17980980
Of course! Try this:

@echo off
setlocal enabledelayedexpansion

cd Data

set count=0
set FolderCount=-1
set FolderName=Folder!FolderCount!
set NumFilesPerFolder=1000

for %%a in (*.*) do (
      if !count! lss 100000 set NewName=test.!count!
      if !count! lss 10000 set NewName=test.0!count!
      if !count! lss 1000 set NewName=test.00!count!
      if !count! lss 100 set NewName=test.000!count!
      if !count! lss 10 set NewName=test.0000!count!

      set /a Result=!count! %% !NumFilesPerFolder!

      if !Result!==0 (
            set /a FolderCount=!FolderCount!+1
            set FolderName=Folder!FolderCount!
            mkdir !FolderName!
      )

      move %%a !FolderName!\!NewName!

      set /a count=!count!+1
)

cd ..

endlocal
0
 

Author Comment

by:fmew
ID: 17985942
Hi Raymun,

Great again.
Now that the files are moved to different dirs, the extensions dont have to be a number.
In fact, I need them all to become <OrignalFileName>.jpg

I tried myself, but I do not succeed

Erik
0
 

Author Comment

by:fmew
ID: 17986162
Hi Raymun,

I tried again, and it works now. I did this:
@echo off
setlocal enabledelayedexpansion

cd\
cd abc

ren *.* *.jpg
set count=0
set ext="jpg"
set FolderCount=-1
set FolderName=Plaatjes!FolderCount!
set NumFilesPerFolder=300

for %%a in (*.*) do (
         
          set /a Result=!count! %% !NumFilesPerFolder!

     if !Result!==0 (
          set /a FolderCount=!FolderCount!+1
          set FolderName=Plaatjes!FolderCount!
          mkdir !FolderName!

    )
     
     move %%a !FolderName!\!NewName!

     set /a count=!count!+1
)

cd ..

endlocal

Thx Erik
0
 

Author Comment

by:fmew
ID: 17986336
Wait,

I did something wrong.
It only renames to jpg
So I still need an answer Raymon

Erik
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17987791
Just a minor change.

Change:

move %%a !FolderName!\!NewName!

to

move %%a !FolderName!\%%a
0
 

Author Comment

by:fmew
ID: 17988012
Thx Raymun

This works fine for me

Erik
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now