Monday 25 December 2017

windows - How do I rename multiple files by removing a particular name, whitespace and add a unique string?


I have n number of files I want to rename by removing a particular name, whitespace, and I want to add a unique string with the new name:


E.g.


"MPKL 100055.jpg" 
"MPKL 200452.jpg"
"MPKL 500002_thumb.jpg"

I want to rename the above files to:


"00100055.jpg"
"00200452.jpg"
"00500002_thumb.jpg"

Two zeros have to come in front of the existing number.



Answer



In the below example I used a FOR /F loop to set delimiters and tokens parsing file name parts and use those to set variables for later use with the REN command. I've also utilized the Setlocal EnableDelayedExpansion to handle the expanding of the variables set in the loop accordingly to be properly used (not parsed at runtime) with the rename command per iteration within the loop.


@ECHO ON

SETLOCAL ENABLEDELAYEDEXPANSION
SET SourceDir=C:\Users\User\Desktop\Tester

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



Further Resources



No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...