1

I have a number of files to rename.

Eg: ERKS 400001_thumb.jpg
    ERKS 500124_thumb.jpg
    ERKS 100004_thumb.jpg

I want to rename the above files as:

Eg: 00400001.jpg
    00500124.jpg
    00100004.jpg

I used this code, but I could not get the result I need.

@ECHO ON

SETLOCAL ENABLEDELAYEDEXPANSION
SET SourceDir=F:\Square.....

FOR /F "TOKENS=1-3 DELIMS=. " %%F IN ('DIR /B /A-D "%SourceDir%\*.jpg"') DO (
  SET "part1=%%~F" 
  SET "part2=%%~G" 
  SET "part3=%%~H"
  REN "%SourceDir%\!part1! !part2!.!part3!" "00!part2!.!part3!"
)
GOTO EOF

If I run the above code the result is this:

Eg: 00400001_thumb.jpg
    00500124_thumb.jpg
    00100004_thumb.jpg
How can I rename the files?

  • I have this tucked away and I use if from time to time. Give me a sec and I'll find it and post it up for you. Thanks for working on it and showing me what you're up to. – ejbytes Sep 22 '16 at 5:30
1

You will want to use the below syntax for what you explain in this particular case. You have to add an additional delimiter underbar _ character and also add one more token so 1-4. With the additional token and delimiter, I added the part4 variable to give you the expected result.

Since this seems to be related to my answer for you on this post, I figured I'd help with this too.

All JPG Files

@ECHO ON

SETLOCAL ENABLEDELAYEDEXPANSION
SET SourceDir=F:\Square.....

FOR /F "TOKENS=1-4 DELIMS=._ " %%F IN ('DIR /B /A-D "%SourceDir%\*.jpg"') DO (
  SET "part1=%%~F" 
  SET "part2=%%~G" 
  SET "part3=%%~H"
  SET "part4=%%~I"
  REN "%SourceDir%\!part1! !part2!_!part3!.!part4!" "00!part2!.!part4!"

)
GOTO EOF

If you have a folder with JPG files in it with the word "thumb" in them and others without the word "thumb" in them, you can use this script below to only change those with the the word "thumb" in them as you describe in your question.

Thumb Named JPG Files Only

@ECHO ON

SETLOCAL ENABLEDELAYEDEXPANSION
SET SourceDir=F:\Square.....

FOR /F "TOKENS=1-4 DELIMS=._ " %%F IN ('DIR /B /A-D "%SourceDir%\*thumb.jpg"') DO (
  SET "part1=%%~F" 
  SET "part2=%%~G" 
  SET "part3=%%~H"
  SET "part4=%%~I"
  REN "%SourceDir%\!part1! !part2!_!part3!.!part4!" "00!part2!.!part4!"
  ECHO "%SourceDir%\!part1! !part2!!part3!.!part4!" "00!part2!.!part4!"
)
GOTO EOF

NOTE: enter image description here


Further Resources

  • This code gave me exact output. Thanks @Honey_D_Clown_IT – Ezhilarasan Sep 22 '16 at 6:54
1

Next code snippet shows two possible solutions:

Script (note that operational RENAME command is merely displayed using ECHO for debugging and demonstrating purposes):

@ECHO OFF >NUL
SETLOCAL EnableExtensions DisableDelayedExpansion

SET "SourceDir=D:\bat\SU\Files\1126981" change to meet your circumstances

echo if _thumb part is fixed
FOR /F "TOKENS=1-3 DELIMS=. " %%F IN ('DIR /B /A-D "%SourceDir%\*.jpg"') DO (
  SET "part1=%%~F" 
  SET "part2=%%~G" 
  SET "part3=%%~H"
  SETLOCAL EnableDelayedExpansion
    echo REN "%SourceDir%\!part1! !part2!.!part3!" "00!part2:_thumb=!.!part3!"
  ENDLOCAL
)

echo(
echo if _thumb part could vary, e.g. _thumb, _thumbnail, _thumbsketch
FOR /F "TOKENS=*" %%g IN ('DIR /B /A-D "%SourceDir%\*.jpg"') DO (
  FOR /F "TOKENS=1-4 DELIMS=_. " %%F IN ("%%~g") DO (
    echo REN "%SourceDir%\%%~g" "00%%~G.%%~I"
  )
)

Output:

==> D:\bat\SU\1126981.bat
if _thumb part is fixed
REN "D:\bat\SU\Files\1126981\ERKS 100004_thumb.jpg" "00100004.jpg"
REN "D:\bat\SU\Files\1126981\ERKS 400001_thumb.jpg" "00400001.jpg"
REN "D:\bat\SU\Files\1126981\ERKS 500124_thumb.jpg" "00500124.jpg"
REN "D:\bat\SU\Files\1126981\ERKS 900009_thumbnail!.jpg" "00900009nail!.jpg"

if _thumb part could vary, e.g. _thumb, _thumbnail, _thumbsketch
REN "D:\bat\SU\Files\1126981\ERKS 100004_thumb.jpg" "00100004.jpg"
REN "D:\bat\SU\Files\1126981\ERKS 400001_thumb.jpg" "00400001.jpg"
REN "D:\bat\SU\Files\1126981\ERKS 500124_thumb.jpg" "00500124.jpg"
REN "D:\bat\SU\Files\1126981\ERKS 900009_thumbnail!.jpg" "00900009.jpg"

==>
0

It's a piece of code I've had for a while now. Prompts you to enter suffix (jpg) then string target (_thumbs) and finally replacement string (leave blank for replacing string with empty string).

@echo off
Setlocal enabledelayedexpansion

ECHO Enter the suffix target (e.g. txt, jpg):
Set /P Suffix= 
ECHO Enter Target String:
Set /P Pattern=
ECHO Enter Replacement String (or leave blank):
Set /P Replace=

For %%# in ("*.%Suffix%") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

In your case: run this batch file 1st time: target "ERKS " replacement "00"; without quotations. The second run: target "_thumbs" replaced with "". Just hit ENTER without entering anything if you want the replacement to be blank (no char).

0

If you got

00400001_thumb.jpg 00500124_thumb.jpg 00100004_thumb.jpg

the rest is a piece of cake. Since your files appear to be the same char length, just do a

ren *.jpg ????????.jpg

and you will have :

00400001.jpg 00500124.jpg 00100004.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.