Replace Word In Filename With Another Word

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Replace Word In Filename With Another Word

#1 Post by data808 » 19 Dec 2023 16:00

I have a ton of PDF files in a folder. It is named in this format:

ABC - Deposit 07-05-23.pdf

I would like a batch file to replace the "- Deposit" with "TALLY" instead.

If possible, I would also like the date format to be replaced from MM/DD/YY.pdf to MM.DD.YY.pdf. So basically remove the dashes in the date to periods instead.

Thank you for the help on this.

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

Re: Replace Word In Filename With Another Word

#2 Post by data808 » 19 Dec 2023 16:57

For reference of what I would like the result to be:

Original File Name
ABC - Deposit 07-05-23.pdf

New File Name:
ABC TALLY 07.05.23.pdf

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Replace Word In Filename With Another Word

#3 Post by Squashman » 19 Dec 2023 22:17

Again this is basic string substitution. Your last two forum threads covered both of what you want to do. What don't you understand?

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

Re: Replace Word In Filename With Another Word

#4 Post by Batcher » 20 Dec 2023 03:30

Code: Select all

@echo off
cd /d "%~dp0"
for /f "delims=" %%i in ('dir /b /s /a-d *-*.pdf') do (
    set "OldFile=%%i"
    set "OldName=%%~nxi"
    setlocal enabledelayedexpansion
    set NewName=!OldName:- Deposit=TALLY!
    set NewName=!NewName:-=.!
    ren "!OldFile!" "!NewName!"
    endlocal
)
pause

Post Reply