Computer Hope
Microsoft => Microsoft DOS => Topic started by: uavaus on September 06, 2012, 09:29:29 PM
-
Hi!
I created a batch file that loops through a list of files in a folder, strips a part of each one and renames it (see example below)
Batch file:
for %%i in (*.pdf) do (set fName=%%i)
ren %fName% %fName:~-10%
Original list:
John Smith_12_123456.pdf
Brian Smith_10_456789.pdf
Joshua Smith_17_765436.pdf
Kate Noname_09_786526.pdf
Output:
123456.pdf
456789.pdf
765436.pdf
786526.pdf
However, if there is a file with brackets in the folder (ex.: John_(Paul)_Smith_12_123456.pdf , the batch file does not work and throws an error message. How can I fix it?
Is there a way to replace brackets for a different character (underscore would be ideal)?
Thank you.
-
This should work (untested):
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (' dir *.pdf /b ') do (
set "fName=%%i"
ren "!fName!" "!fName:~-10!"
)
-
This should work (untested):
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (' dir *.pdf /b ') do (
set "fName=%%i"
ren "!fName!" "!fName:~-10!"
)
Thank you so much mate! This script does exactly what I wanted it to do.