The usage of international date format YYYY-MM-DD
was a wise decision as it makes it possible to compare dates by comparing the date strings.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define here the folder containing the PDF file. The folder path must end
rem with a backslash. By default is used the folder path of the batch file.
set "Folder=%~dp0"
rem Get current date region independent and change format to YYYY-MM-DD.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "FileNameDate=%%I"
set "FileNameDate=%FileNameDate:~0,4%-%FileNameDate:~4,2%-%FileNameDate:~6,2%"
rem Process all *.pdf files starting with a date in specified folder. The
rem inner FOR splits the current file name up on first space which means
rem the date string is assigned to loop variable J. This date string is
rem compared as string with the current date string character by character.
rem If a character in date string of current file has a lower code value
rem than the corresponding character in current date string, the function
rem strcmp used internally by cmd.exe for the string comparison returns a
rem negative number and the IF condition is true as the string comparison
rem result is less 0. The IF condition is also true if the two compared
rem strings are equal because of strcmp returns in this case 0 which is
rem less or equal value 0 used by command IF on comparing two strings
rem with LEQ as comparison operator.
for /F "delims=" %%I in ('dir "%Folder%????-??-??*.pdf" /A-D-H /B 2^>nul') do (
for /F %%J in ("%%I") do if "%%J" LEQ "%FileNameDate%" ren "%Folder%%%I" "EXPIRED %%I"
)
endlocal
For an explanation of first FOR loop read my answer on %date% produces different result in batch file.
Note: This batch file requires that there is a space character in the file name between date and rest of the file name and of course the date format is YYYY-MM-DD
.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
if /?
rem /?
ren /?
set /?
setlocal /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?