Computing.Net > Forums > Programming > Rename Files Per Rules

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Rename Files Per Rules

Reply to Message Icon

Name: transitionality2
Date: September 17, 2008 at 14:51:27 Pacific
OS: Windows XP Pro SP3
CPU/Ram: AMD Athlon 1800+ 512 MB
Product: N/A
Comment:

Let's assume we have three groups of files, a, b and c:

a1.jpg
a2.jpg
...
a50.jpg
b1.jpg
b2.jpg
...
b150.jpg
c1.jpg
c2.jpg
...
c20.jpg

Let us assume that each group of a, b or c can have at most 999 files. We want these files to be renamed such that:

* c files are prefixed with 0-, a files are prefixed with 1-, and b files are prefixed with 2- to create a desired alphabetical order.

* The digit components of the filenames are completed to three digits using preceding zeroes if necessary.

So the above files would be renamed to the following:

0-c001.jpg
0-c002.jpg
...
0-c020.jpg
1-a001.jpg
1-a002.jpg
...
1-a050.jpg
2-b001.jpg
2-b002.jpg
...
2-b150.jpg

How would one go about doing this with a batch file? Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: September 18, 2008 at 16:24:41 Pacific
Reply:

Hi
Try This
I inserted echo before the ren command for safety.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /b *.jpg') do (
set FName=%%~na
set FChar=!FName:~,1!
set /a Num=!FName:~1!
if !Num! LSS 10 set Num=0!Num!
if !Num! LSS 100 set Num=0!Num!
if !FChar!==c set NName=0-!FChar!!Num!
if !FChar!==a set NName=1-!FChar!!Num!
if !FChar!==b set NName=2-!FChar!!Num!
echo ren !FName!.jpg !NName!.jpg
)


0

Response Number 2
Name: transitionality2
Date: September 19, 2008 at 03:33:36 Pacific
Reply:

Thank you for your help. The code works great. I especially like the clever bit where you padded the numbers by testing iteratively in two lines. It would have taken me a lot more lines.

There was a little bug where, for *.jpg files in the same directory that don't have any of the predetermined prefixes (a, b or c) the batch file tried to name all of them 0-c001.jpg. I think it was doing that because NName was uninitialized and it tried to initialize NName somehow. It doesn't make sense but that's what it did. I fixed it by adding a couple of lines to initialize NName and then test for it as follows:

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /b *.jpg') do (
set FName=%%~na
set NName=!FName!
set FChar=!FName:~,1!
set /a Num=!FName:~1!
if !Num! LSS 10 set Num=0!Num!
if !Num! LSS 100 set Num=0!Num!
if !FChar!==c set NName=0-!FChar!!Num!
if !FChar!==a set NName=1-!FChar!!Num!
if !FChar!==b set NName=2-!FChar!!Num!
if not !NName!==!FName! echo ren !FName!.jpg !NName!.jpg
)


0

Response Number 3
Name: dtech10
Date: September 19, 2008 at 14:13:42 Pacific
Reply:

Hi
Glad it worked.
The extra code you added was also a neat
solution.
DTech10


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Strange problem in batch batch script to upload to...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Rename Files Per Rules

Renaming files www.computing.net/answers/programming/renaming-files/11132.html

Renaming files using a batch www.computing.net/answers/programming/renaming-files-using-a-batch/11819.html

Renaming files www.computing.net/answers/programming/renaming-files/11768.html