Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

dos batch rename file extension

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
fmew
Asked:
fmew
1 Solution
 
fostejoCommented:
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
 
AlexNekCommented:
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
 
RaymunCommented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
RaymunCommented:
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
 
fmewAuthor Commented:
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
 
RaymunCommented:
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
 
fmewAuthor Commented:
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
 
fmewAuthor Commented:
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
 
fmewAuthor Commented:
Wait,

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

Erik
0
 
RaymunCommented:
Just a minor change.

Change:

move %%a !FolderName!\!NewName!

to

move %%a !FolderName!\%%a
0
 
fmewAuthor Commented:
Thx Raymun

This works fine for me

Erik
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now