This batch code could be used to find out which file has the newest date in file name for the three example file names:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "NewestFile="
set "NewestDate=0000-00-00"
for %%I in ("file*.doc") do (
set "FileName=%%~nI"
if "!FileName:~-8,1!" == "." (
set "FileDate=!FileName:~-4!-!FileName:~-7,2!-!FileName:~-10,2!"
) else (
set "FileDate=20!FileName:~-2!-!FileName:~-5,2!-!FileName:~-8,2!"
)
echo Date is !FileDate! in file name for file: "%%I"
if "!FileDate!" GTR "!NewestDate!" (
set "NewestDate=!FileDate!"
set "NewestFile=%%I"
)
)
if defined NewestFile (
echo File "%NewestFile%" is the newest with date: %NewestDate%
) else (
echo Could not find any file matching the pattern file*.doc.
)
endlocal
pause
The batch file simple evaluates the eighth character of the file name without file extension from the right side (end of file name) to determine if the date at end of file name is in format DD.MM.YYYY
assigned to environment variable FileDate
in international date format YYYY-MM-DD
or in format DD.MM.YY
assigned to the environment variable also in international date format YYYY-MM-DD
with using 20
as century using string substitutions.
Then this date string is compared case-sensitive with the date string of newest file. If one character in current file date string is greater than the same character in currently newest date string, the current file has a newer date than the currently newest file. The big advantage of using international date format is that a string comparison can be used to find out which date is newer respectively older.
The batch file above does not work for file names containing one or more !
. The command line set "FileName=%%~nI"
is parsed twice by Windows command processor before execution because of enabled delayed environment variable expansion. See also How does the Windows Command Interpreter (CMD.EXE) parse scripts? Therefore each exclamation mark in file name is interpreted as begin/end of a delayed environment variable reference resulting in assigning the file name with !
not correct to the environment variable.
One solution for working code for file names with exclamation marks is not using delayed expansion by using a subroutine:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "NewestFile="
set "NewestDate=0000-00-00"
for %%I in ("file*.doc") do call :ProcessFileName "%%I"
if defined NewestFile (
echo File "%NewestFile%" is the newest with date: %NewestDate%
) else (
echo Could not find any file matching the pattern file*.doc.
)
endlocal
pause
goto :EOF
:ProcessFileName
set "FileName=%~n1"
if "%FileName:~-8,1%" == "." (
set "FileDate=%FileName:~-4%-%FileName:~-7,2%-%FileName:~-10,2%"
) else (
set "FileDate=20%FileName:~-2%-%FileName:~-5,2%-%FileName:~-8,2%"
)
echo Date is %FileDate% in file name for file: %1
if "%FileDate%" GTR "%NewestDate%" (
set "NewestDate=%FileDate%"
set "NewestFile=%~1"
)
goto :EOF
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.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
set /?
setlocal /?
See also Where does GOTO :EOF return to?