1

I need help writing a batch file to open the most recent file in a folder destination where the file is updated weekly. The file has other functions as well, but this part I am struggling on. The format of the files is as below:

file name 27.08.18.doc with one created weekly at the start of the week.

At the moment my directory contains for example the files file name 06.08.18.doc, file name 13.08.2018.doc and file name 20.08.2018.doc.

I tried messing around with this answer I found on another question, but its not working and I'm not really experienced enough to know why.

setlocal enabledelayedexpansion
set max=0
for %%x in (file*.doc) do (
    set "FN=%%~nx"
    set "FN=!FN:file name =!"
    if !FN! GTR !max! set max=!FN!
)
echo highest version: file name %max%.doc

Once I've got that variable %max% to provide the day or date of the most recent file (27 in the example), I can work with that myself. I'm hoping someone can provide a response and explain what the command does, to help me learn & fix my problem at the same time.

P Meddyyy
  • 13
  • 3

1 Answers1

0

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?

Mofi
  • 38,783
  • 14
  • 62
  • 115