Page 1 of 1

Batch file to replace - with . in file name

Posted: 04 Dec 2023 13:28
by data808
Anyone know how to make a batch file to replace all dashes (-) in a file name with periods (.)?

I basically have a ton of PDF files that are named like this:

HN TALLY 11-18-23.pdf
HN TALLY 11-19-23.pdf
REG 11-20-23.pdf
EW CAR 11-21-23.pdf
and so on....

I would like it to rename the files like this instead:

HN TALLY 11.18.23.pdf
HN TALLY 11.19.23.pdf
REG 11.20.23.pdf
EW CAR 11.21.23.pdf

Is that possible? Thanks for any help you can provide.

Re: Batch file to replace - with . in file name

Posted: 04 Dec 2023 20:41
by Batcher
CurrentFolderOnly.bat

Code: Select all

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

Re: Batch file to replace - with . in file name

Posted: 04 Dec 2023 20:41
by Batcher
CurrentAndSubFolder.bat

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:-=.!
    ren "!OldFile!" "!NewName!"
    endlocal
)

Re: Batch file to replace - with . in file name

Posted: 05 Dec 2023 11:59
by Squashman
data808 wrote:
04 Dec 2023 13:28
Anyone know how to make a batch file to replace all dashes (-) in a file name with periods (.)?

I basically have a ton of PDF files that are named like this:

HN TALLY 11-18-23.pdf
HN TALLY 11-19-23.pdf
REG 11-20-23.pdf
EW CAR 11-21-23.pdf
and so on....

I would like it to rename the files like this instead:

HN TALLY 11.18.23.pdf
HN TALLY 11.19.23.pdf
REG 11.20.23.pdf
EW CAR 11.21.23.pdf

Is that possible? Thanks for any help you can provide.
In your last question you were shown how to do string replacement. What was stopping you from attempting this on your own?

Re: Batch file to replace - with . in file name

Posted: 19 Dec 2023 19:16
by data808
Thanks Squashman for pointing that out. I found that post and tried it myself with this code:

@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:-=.!
ren "!OldFile!" "!NewName!"
endlocal
)

Here is the result:

Original File:
ABC - Deposit 12-08-23.pdf

After running batch file:
ABC . Deposit 12.08.23.pdf

What I want it to be:
ABC TALLY 12.08.23.pdf

Would you know how to do this?

Re: Batch file to replace - with . in file name

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

Re: Batch file to replace - with . in file name

Posted: 20 Dec 2023 10:53
by Squashman
Batcher wrote:
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
You should just change your name to BatchGPT.