Batch rename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Batch rename

#1 Post by drgt » 29 May 2020 03:09

Hi
I need to batch rename a bunch of files that are located in one directory and have a name like this: "GF6DA7BXG44DO7BRGU4DQNJZG44DKNRRG42HYSKNI5PTEMBSGAYDIMRYL4ZDAMRTGIZXYLTKOBTXY2LNMFTWKL3KOBSWO7DOOVWGY"
to 0001.jpg and so on till all processed.

Would you please post here the batch file code?
Thank you very much

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch rename

#2 Post by dbenham » 29 May 2020 05:50

This is trivial with JREN.BAT - a hybrid JScript/batch regular expression renaming utility

Code: Select all

call jren "^.*" "lpad($n,'0000')+'.jpg'" /j /p "c:\pathToYourFolder" /rfm "^[a-z0-9]+$"
It is not much more complicated to use pure batch

Code: Select all

@echo off
setlocal enableDelayedExpansion
pushd "c:\pathToYourFolder"
for /f "delims=: tokens=1*" %%A in (
  '"dir /b /a-d | findstr /x [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]* | findstr /n ."'
) do (
  set "name=0000%%A"
  echo "%%B"  --^>  "!name:~-4!.jpg"
  ren "%%B" "!name:~-4!.jpg"
)
popd

Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch rename

#3 Post by dbenham » 29 May 2020 06:38

I originally forgot to left zero pad the number to 4 digits. I edited my prior post to add the padding

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Batch rename

#4 Post by drgt » 29 May 2020 10:39

Thanks a million!

Post Reply