Partially rename files with calc'd value

September 28, 2010 at 18:15:55
Specs: Windows XP, 1.8G Processor; 2G RAM
I have significant DOS experience and have written many simple batch files, but I don't know how to do this job.

I have 200 photos in a single folder. I want to rename each file, retaining all but the first four characters of the current file name.

I need to somehow capture the filename. Then replace the first four characters of the file name with a numeric incremented value, starting with 0100, and incremented by 20 for each subsequent filename. Example:

Original Filenames:
a3pt propty.jpg
b127 profh3.jpg
b29f p001067,jpg

After renaming them, the Filenames should be:
0100 propty.jpg
0120 profh3.jpg
0140 p001067,jpg

I don't need to process the photos, only need to change the first four characters of filename with an incremented value.

Can someone please show me how to do it?

Thanks. Bob Kunz


See More: Partially rename files with calcd value

Report •


#1
September 28, 2010 at 18:57:52
download gawk for windows then do this
C:\test>dir /b *jpg | gawk "BEGIN{n=80}{n=sprintf(\"%04s\",n+=20);new=n substr($0,5); cmd=\"ren \\\"\"$0\"\\\" \\\"\"new
\"\\\"\";system(cmd)}"

GNU win32 packages | Gawk


Report •

#2
September 29, 2010 at 08:16:36
Your recommendation far exceeds my ability. You have assumed my computer literacy to be much greater than reality.

As suggested, I downloaded - and installed - CoreUtils for Windows. Next, you say "do this" followed by a long string of text which I assumed should be entered into a CMD or COMMAND window. But after 80 characters were typed, a "Beep" let me know I could not further proceed. Pressing Enter to start the next line resulted in execution of the first 80 characters, which obviously failed.

I then assumed that I must first invoke CoreUtils, and enter your text string into it, but I could not find a way to open CoreUtils. Searching for an "exe" file, I found 98 of them, so that didn't help either.

I next pursued the CoreUtils documentation manuals and 'help' files, but that too far exceeded by abilities.

Needless to say, I'm stumped. Can you further clarify or is this whole thing above my capability to understand -- which is highly probable.

Thank you.

Bob Kunz

Can you clarify


Report •

#3
September 29, 2010 at 17:10:40
Hi Bob K. Please test this:

@echo off
cls
setlocal enabledelayedexpansion

set /a cntr=100

pushd Path\to\.jpg files\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=1*" %%1 in ('dir /on /b *.jpg') do (
    ren "%%1 %%2" "0!cntr! %%2"&& set /a cntr+=20
)
popd


Report •

Related Solutions

#4
September 29, 2010 at 17:52:29
@Bobotk,
>> As suggested, I downloaded - and installed - CoreUtils for Windows.

Sorry I gave wrong link.
you should download gawk for windows instead. (But coreutils has many useful tools so keep it in your arsenal).

And you can cut and paste the command i gave into a batch file and run. don't have to type it every time


GNU win32 packages | Gawk


Report •

#5
September 29, 2010 at 18:00:18
@Wahine,

if file names don't have spaces, it doesn't work. So maybe you can take care of that as well.

C:\test>dir /b *jpg
a3pt propty.jpg
b29f p001067.jpg
propty.jpg
test.jpg

C:\test>test.bat
ren "a3pt propty.jpg" "0100 propty.jpg"
ren "b29f p001067.jpg" "0120 p001067.jpg"
ren "propty.jpg " "0140 "
ren "test.jpg " "0160 "

GNU win32 packages | Gawk


Report •

#6
September 29, 2010 at 19:26:12
Sure can but I'm sure Bob would have indicated the lack of a space in the filenames in his post.

However if you insist:

@echo off
cls
setlocal enabledelayedexpansion

set /a cntr=100

pushd Path\to\.jpg files\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=*" %%1 in ('dir /on /b *.jpg') do (
    set infile=%%1
    set infile=!infile:~4!
    ren "%%1" "0!cntr!!infile!"&&set /a cntr+=20
)

And you will no doubt note that I have omitted to confirm that the filename (without extension) contains 4 or more characters which means that if it does not the resultant filename could be 0100pg or some such combo. How far need we go in trying to ensure that the OP has posted relevant information?


Report •

Ask Question