Windows batch script to find most recently modified file

I needed to find and operate on the most recently modified file in a directory, but from the Windows XP command line rather than a Linux shell script. Knowing how unexpectedly powerful (but so very arbitrary and convoluted!) the Windows command line can be, I figured it was possible with a bit of help file reading.

And lo, it was good:

@echo off
:: This temp batch file is used to execute a subroutine.
set TMP_RECENT_BAT=%TEMP%\tmp_recent.bat

:: Reset the "output" env var in case something goes wrong
set MOST_RECENT_FILE=

:: Create the temp batch file.
::
:: This subroutine lists all files in the current directory,
:: sorts it in descending last-modified-time order,
:: and strips out the topmost file (i.e. most recently modified).
::
:: Directories, system files, and hidden files are not considered.
::
:: A side-effect of this routine is that the filename has a prefix of "[1]"
echo @echo off > %TMP_RECENT_BAT%
echo dir /o-d /a-d-h-s /b %1 ^| find ^"^" /v /n ^| find ^"[1]^" >> %TMP_RECENT_BAT%
:: Execute the subroutine above, and grab the output in an environment var.
:: We are assuming the subroutine above correctly returns only one line.
for /f "usebackq delims=="%%i in (`%TMP_RECENT_BAT%`) do set TMP_RECENT=%%i  

:: Strip the first 3 chars of the return value of the subroutine, i.e. "[1]"
::
:: If the subroutine didn't return a good value, don't try to set the var,
:: b/c we'll get a string like "~3" instead of a good value.
if "%TMP_RECENT%" == "" goto skip  set MOST_RECENT_FILE=%TMP_RECENT:~3%
:: This line not really necessary, since script that use this can just access
:: the environment variable named "MOST_RECENT_FILE".
echo %MOST_RECENT_FILE%
:skip
set TMP_RECENT=  

:: Delete the temp file
del %TMP_RECENT_BAT%
Advertisement

2 Responses to “Windows batch script to find most recently modified file”

  1. deadazzazin Says:

    The above actually brought me here… will try above [need the modified time not the filename though] and funny enough was thinking of programming something like the MouseGesture. Thanks 4 that, will use it 2 my ADV. browsed the blog, nice pics esp the food!

  2. PatrickMc Says:

    Have you looked at biterscripting ? There is a good script posted at http://www.biterscripting.com/LearningScripting/Lesson4.html . It is called latest.txt and it shows the latest created file. (It is actually a tutorial for scripting.) There is also a script posted called latestmod.txt that finds the latest modified file within a folder. It is in one of the question-answer.

    Patrick


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.