Batch File To Change Date Format In Filename
Moderator: DosItHelp
Batch File To Change Date Format In Filename
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.
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
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
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.
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
Try this:
I assumed that all files with *.pdf extension must be processed. If this is not true, a small fix is needed
Antonio
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"
)
)
Antonio
Re: Batch File To Change Date Format In Filename
Always good to have a testing strategy.data808 wrote: ↑04 Jan 2024 12:07Thank 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.
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"
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
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":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"
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"
Re: Batch File To Change Date Format In Filename
I didn't want to assume all the files had a space before the date.Aacini wrote: ↑04 Jan 2024 17:02It 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":
AntonioCode: 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"
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
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.Squashman wrote: ↑04 Jan 2024 15:15Always good to have a testing strategy.data808 wrote: ↑04 Jan 2024 12:07Thank 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.
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.The best thing you can do is start reading the help files for the commands that people are giving you the code for.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"
Blatantly trusting the code that strangers give to you on the Internet is not a good strategy.
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.