Solved

dos batch rename file extension

Posted on 2006-11-19
11
373 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

19 Experts available now in Live!

Get 1:1 Help Now