Batch file to replace - with . in file name

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 replace - with . in file name

#1 Post by data808 » 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.

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

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

#2 Post by Batcher » 04 Dec 2023 20:41

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
)

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

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

#3 Post by Batcher » 04 Dec 2023 20:41

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
)

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

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

#4 Post by Squashman » 05 Dec 2023 11:59

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?

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

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

#5 Post by data808 » 19 Dec 2023 19:16

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?

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

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

#6 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

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

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

#7 Post by Squashman » 20 Dec 2023 10:53

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.

Post Reply