Wednesday, March 4, 2015

windows - How to rename by removing last part name for multiple files


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?


Answer



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


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...