Thursday, August 27, 2015

windows - Batch Script to Bulk Rename Files - Strip off Parenthesis and all Characters between


I wish to rename a group of files in a particular folder in such a way that everything between and including the parenthesis is erased.


For example a file with the name of:


"78 Tax Tips for Canadians for Dummies (ISBN - 0470676582)"

I wish to rename it to:


"78 Tax Tips for Canadians for Dummies"

I need to do this for all the files file in a particular folder for any and all files that follow this same pattern for it's file name characters:


Dummies (ISBN - .........)


I tried following and using the batch script logic on this post but I could not get that to work the number after the ISBN - ########## part keeps changing with the way I did that incorrectly.


Answer




Batch file rename, strip off parenthesis and all characters between, but keep extension



Save all the logic from the below example to a text file and name it something.cmd and then simply execute it. . .


Script Notes



  • Where SET RenDir=C:\Path is C:\Path, you'll need to put the full
    path where these files you wish to rename exist.


  • The assumption with this script logic is there will ONLY be files in
    the RenDir with parenthesis () that will need to be stripped with
    the (ISBN - .........) type pattern and no other parts of any of
    those file names will contain any parenthesis ().



Example Batch Script Logic


@ECHO ON
SET RenDir=C:\Path
FOR /F "DELIMS=() TOKENS=1,3" %%F IN ('DIR /B /A-D "%RenDir%\*.*"') DO (
CALL :RenameFiles "%%~F" "%%~G"
)
GOTO EOF
:RenameFiles
SET fname=%~1
SET Ext=%~2
REN "%RenDir%\%fname%*%ext%" "%fname%%ext%"
GOTO EOF

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...