Page 1 of 1

Batch File To Change Date Format In Filename

Posted: 03 Jan 2024 14:20
by data808
I have PDF files that have dates in the file name. I would like to keep the exact date for each of these files but change the format of the date. Couple examples for how the filenames are read:

AB TALLY 01.01.24.pdf (date is mm.dd.yy)
AB ABCDEFG 12.25.23.pdf (date is mm.dd.yy)

I would like it to change to these filenames to:

AB TALLY 2024.01.01.pdf (date is yyyy.mm.dd)
AB ABCDEFG 2023.12.25.pdf (date is yyyy.mm.dd)

Does anyone have code for a batch file to do this? Thank you for your help on this.

Re: Batch File To Change Date Format In Filename

Posted: 04 Jan 2024 06:48
by Batcher
1.bat

Code: Select all

@echo off
cd /d "%~dp0"
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /b /a-d *.pdf ^| findstr /i /e "[0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"') do (
    for %%b in ("%%~na") do (
        set "yyyy=20%%~xb"
        set "yyyy=!yyyy:.=!"
        for %%c in ("%%~nb") do (
            set "dd=%%~xc"
            set "dd=!dd:.=!"
            set "mm=%%~nc"
            set "NonDate=!mm:~0,-2!"
            set "mm=!mm:~-2!"
        )
    )
    echo "%%a" --^> "!NonDate!!yyyy!.!mm!.!dd!%%~xa"
    ren "%%a" "!NonDate!!yyyy!.!mm!.!dd!%%~xa"
)
pause

Re: Batch File To Change Date Format In Filename

Posted: 04 Jan 2024 12:07
by data808
Thank you very much. It's working great except I had some dates that were already formatted correctly and those got messed up.

Is there a way you could add a feature that if the date is already in YYYY.MM.DD format, just leave it alone? If not, no worries. I will just make a note in the code to be careful next time I use it.

Thanks again.

Re: Batch File To Change Date Format In Filename

Posted: 04 Jan 2024 13:51
by Aacini
Try this:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in ('dir /A:-D /B *.pdf') do (
   set "name=%%a"
   for %%b in (%%a) do (
      set "head=!name: %%b=!"
      for /F "tokens=1-3 delims=." %%x in ("%%b") do set "M=%%x" & set "D=%%y" & set "Y=%%z"
   )
   if "!M:~2!" equ "" (
      ECHO ren "%%a" "!head! 20!Y!.!M!.!D!.pdf"
      ren "%%a" "!head! 20!Y!.!M!.!D!.pdf"
   ) else (
      echo Not renamed: "%%a"
   )
)
I assumed that all files with *.pdf extension must be processed. If this is not true, a small fix is needed

Antonio

Re: Batch File To Change Date Format In Filename

Posted: 04 Jan 2024 15:15
by Squashman
data808 wrote:
04 Jan 2024 12:07
Thank you very much. It's working great except I had some dates that were already formatted correctly and those got messed up.

Is there a way you could add a feature that if the date is already in YYYY.MM.DD format, just leave it alone? If not, no worries. I will just make a note in the code to be careful next time I use it.

Thanks again.
Always good to have a testing strategy.
You may want to consider understanding the code you are using before using it.
The reason the code you used screws up YYYY.MM.DD files is because it matches any files that end in the ##.##.##.pdf. That will match YYYY.MM.DD.pdf.
You could pipe to another FINDSTR command to not match files ending in ####.##.##.pdf.

Code: Select all

dir /b /a-d *.pdf ^| findstr /i /e "[0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"^|findstr /v /i /e "[0-9][0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"
The best thing you can do is start reading the help files for the commands that people are giving you the code for.
Blatantly trusting the code that strangers give to you on the Internet is not a good strategy.

Re: Batch File To Change Date Format In Filename

Posted: 04 Jan 2024 17:02
by Aacini
Squashman wrote:
04 Jan 2024 15:15

. . . . .

Always good to have a testing strategy.
You may want to consider understanding the code you are using before using it.
The reason the code you used screws up YYYY.MM.DD files is because it matches any files that end in the ##.##.##.pdf. That will match YYYY.MM.DD.pdf.
You could pipe to another FINDSTR command to not match files ending in ####.##.##.pdf.

Code: Select all

dir /b /a-d *.pdf ^| findstr /i /e "[0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"^|findstr /v /i /e "[0-9][0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"
. . . . .
It is simpler to just indicate findstr to match files that start in a space, followed by ##.##.##.pdf, so it just match " MM.DD.YY.pdf" and not " YYYY.MM.DD":

Code: Select all

dir /b /a-d *.pdf ^| findstr /i /e /R /C:" [0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"
Antonio

Re: Batch File To Change Date Format In Filename

Posted: 05 Jan 2024 10:25
by Squashman
Aacini wrote:
04 Jan 2024 17:02
It is simpler to just indicate findstr to match files that start in a space, followed by ##.##.##.pdf, so it just match " MM.DD.YY.pdf" and not " YYYY.MM.DD":

Code: Select all

dir /b /a-d *.pdf ^| findstr /i /e /R /C:" [0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"
Antonio
I didn't want to assume all the files had a space before the date.
Maybe use an inverse class isntead?

Code: Select all

dir /b /a-d *.pdf ^| findstr /i /e /R /C:"[^0-9][^0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"

Re: Batch File To Change Date Format In Filename

Posted: 09 Jan 2024 19:56
by data808
Squashman wrote:
04 Jan 2024 15:15
data808 wrote:
04 Jan 2024 12:07
Thank you very much. It's working great except I had some dates that were already formatted correctly and those got messed up.

Is there a way you could add a feature that if the date is already in YYYY.MM.DD format, just leave it alone? If not, no worries. I will just make a note in the code to be careful next time I use it.

Thanks again.
Always good to have a testing strategy.
You may want to consider understanding the code you are using before using it.
The reason the code you used screws up YYYY.MM.DD files is because it matches any files that end in the ##.##.##.pdf. That will match YYYY.MM.DD.pdf.
You could pipe to another FINDSTR command to not match files ending in ####.##.##.pdf.

Code: Select all

dir /b /a-d *.pdf ^| findstr /i /e "[0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"^|findstr /v /i /e "[0-9][0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]\.pdf"
The best thing you can do is start reading the help files for the commands that people are giving you the code for.
Blatantly trusting the code that strangers give to you on the Internet is not a good strategy.
You have a very good point and the thought has crossed my mind in the past about trusting what people give me. I always take a look at the code and try to make sense of it to the best of my ability. Sometimes I am able to figure out enough to make small changes to fit my needs. I also setup a test environment before I run it, should anything happen.

I do want to learn but I feel I may not be able to grasp all of it and to be honest I think I have tried in the past and eventually gave up. If you can show me where to look for something really simple to start with, I would appreciate it. However, if I am not able to understand it, I may be back at doing it the way I have been for years now on this forum? Basically, being a batchGPT? lol You guys have been so helpful and that is what keeps me coming back for assistance. If this wasn't a good source for help, I wouldn't return. I really do appreciate this forum and all the people that have helped me over the years.