Page 1 of 1

Batch File To Remove Word In Filename

Posted: 29 Nov 2023 16:43
by data808
I have a bunch of PDF files in a folder and they are all named like this:

Tally Cash Date 11.29.23.pdf

I would like to just remove the words "Cash" and "Date" in the file name so they all show like this:

Tally 11.29.23.pdf

Does anyone know how to make a batch file that can do this? I looked online and only could find batch files to remove the beginning or ending of a file name.

Any help is appreciated. Thank you.

Re: Batch File To Remove Word In Filename

Posted: 29 Nov 2023 21:18
by Batcher
test-1.bat

Code: Select all

@echo off
cd /d "%~dp0"
for /f "delims=" %%i in ('dir /b /a-d *.pdf ^| findstr /i /c:"Cash" /c:"Date"') do (
    set "OldName=%%i"
    setlocal enabledelayedexpansion
    set "NewName=!OldName:Cash=!"
    set "NewName=!NewName:Date=!"
    set "NewName=!NewName:   = !"
    set "NewName=!NewName:  = !"
    echo "!OldName!" ---^> "!NewName!"
    ren "!OldName!" "!NewName!"
    endlocal
)
pause

Re: Batch File To Remove Word In Filename

Posted: 30 Nov 2023 18:28
by data808
Thank you so much for the help. Works great!