Batch File To Remove Word In Filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
data808
Posts: 47
Joined: 19 Jul 2012 01:49

Batch File To Remove Word In Filename

#1 Post by data808 » 29 Nov 2023 16:43

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.

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Batch File To Remove Word In Filename

#2 Post by Batcher » 29 Nov 2023 21:18

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

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File To Remove Word In Filename

#3 Post by data808 » 30 Nov 2023 18:28

Thank you so much for the help. Works great!

Post Reply