Re-Number files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Re-Number files

#1 Post by jeff p » 22 Feb 2016 12:49

Renumber files?

I have 12,000 images in Z:\Images\Renders that i'd like to re-unumber. its a mix of .jpg's, and tga's.

My current folder looks like this:

IMG_00000.tga
IMG_00001.jpg
IMG_00002.jpg
IMG_00003.jpg
IMG_00004.jpg
IMG_00004.tga
IMG_00005.jpg
IMG_00006.jpg
IMG_00006.tga
etc..

I'd like to strip any information but the incremental number, to look like this:

00001.tga
00002.jpg
00003.jpg
00004.jpg
00005.jpg
00006.tga
00007.jpg
00008.jpg
00009.tga

Is this possible?
Thanks for any help!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Re-Number files

#2 Post by foxidrive » 22 Feb 2016 14:08

It's easy, but the actual filename format needs to be known because it changes the code.

Are the filenames exactly as you show there?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Re-Number files

#3 Post by aGerman » 22 Feb 2016 14:50

Assuming that always the last 5 characters of the file name are the incremental number you may try that code:

Code: Select all

@echo off &setlocal

set "nonum="
for /f "delims=" %%i in ('dir /a-d /b *.tga *.jpg') do (
  set "file=%%i"
  set "name=%%~ni"
  setlocal EnableDelayedExpansion
  for /f "delims=1234567890" %%j in ("!name:~-5!") do set "nonum=%%j"
  if not defined nonum ren "!file!" "!name:~-5!%%~xi"
  endlocal
)

Regards
aGerman

jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Re: Re-Number files

#4 Post by jeff p » 22 Feb 2016 23:46

Thanks for the replies!

Foxdrive, Yes the file name will always remain the same for each set of images to renumber.... In this case it's IMG_ in another set it might be IMGR_ ( if there's a way for me to modify the code such that I can account for an additional character space for a different name, thats easy for me.)

aGerman, I tried your code but was unable to get it to work. (using a set of 100 images from IMG_00000.tga to IMG_00100.jpg I placed the bat file in the same directory as the images...but no luck)

Any ideas?

Thanks for the help!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Re-Number files

#5 Post by foxidrive » 23 Feb 2016 03:25

Launch this and open "Z:\Images\Renders\temp.bat.txt" to check the commands.
If it's ok then rename and launch that temporary batch file.

Code: Select all

@echo off
pushd "Z:\Images\Renders\"
(for %%a in (*.tga *.jpg) do (
for /f "tokens=1,* delims=_" %%b in ("%%a") do echo ren "%%a" "%%c"
))>"temp.bat.txt"
echo check "Z:\Images\Renders\temp.bat.txt" and rename and run.
pause

jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Re: Re-Number files

#6 Post by jeff p » 23 Feb 2016 13:08

Thanks, foxidrive.

Your code removed the first name prefix. ( IMG_) and left the number sequence unchanged. .. However the temp.bat.txt file it created gave me the idea to simply follow that line of reasoning and rename all these manually in the same fashion.

Thanks again for the help.

Cheers
Jeff

[newbie]
Posts: 6
Joined: 07 Feb 2015 15:02

Re: Re-Number files

#7 Post by [newbie] » 28 Feb 2016 05:40

Hi,
here a small oneliner, pase the code in cmd box where the jpg/tga located

Code: Select all

cmd /v:on /c "for /f %i in ('dir /b /a-d "*.jpg" "*.tga"') do @(set "file=%~ni" &set "file=!file:~-4!" &echo ren %i !file!%~xi)"

remove the echo to finally rename
not needed a batch file

Post Reply