0

I have a folder with files in it in the following format:

2018-08-20 Doe J Certificate.pdf

2019-01-17 Smith T Certificate.pdf

What I'd like to do is create a batch file that pulls the date out of the file name and compares it to the current date and then adds the text "EXPIRED" to the front of any files that have a date equal to or previous to the current date. I have no idea how to do that. I've only written one batch file before.

Thanks for the help!

Note!

I did a search and have seen similar questions to this but I'm not very familiar with making batch files so I apologize if this is a repeat question, but what I found in search wasn't specific enough to my situation for me to understand.

Community
  • 1
  • 1
Ben H.
  • 102
  • 6

1 Answers1

1

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 /?
Mofi
  • 38,783
  • 14
  • 62
  • 115
  • I had worked on a **very** similar script that does the same process you took. Because you beat me to it, it's pointless for me to make a post. nicely done however. – John Kens Aug 20 '18 at 06:38
  • That was it! Thanks so much. Now I just need to delve into that and try to understand it myself. – Ben H. Aug 20 '18 at 06:39