Page 1 of 1

Replace Word In Filename With Another Word

Posted: 19 Dec 2023 16:00
by data808
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.

Re: Replace Word In Filename With Another Word

Posted: 19 Dec 2023 16:57
by data808
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

Re: Replace Word In Filename With Another Word

Posted: 19 Dec 2023 22:17
by Squashman
Again this is basic string substitution. Your last two forum threads covered both of what you want to do. What don't you understand?

Re: Replace Word In Filename With Another Word

Posted: 20 Dec 2023 03:30
by Batcher

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