1.

Solve : replace characters in the file name?

Answer» 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):

Code: [Select]@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (' dir *.pdf /b ') do (
set "fName=%%i"
ren "!fName!" "!fName:~-10!"
)
Quote from: foxidrive on September 06, 2012, 11:17:52 PM
This should work (untested):

Code: [Select]@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.


Discussion

No Comment Found