Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This would be lightning quick in linux, but I'm not familiar enough with windows flavour of batch scripting.

Basically I want to look for a series of files matching a certain wildcard, and get the one with the most recent modified date.

I've gotten as far as:

for %%X in (*.exe) do (
REM Do stuff....

But I'm not sure what manner of comparison operators there are, or if there's a better way of doing this.

Anyone offer up any good solutions? Ideally it would involve a vanilla install of Vista; so no special trickery like cygwin/etc.

share|improve this question
up vote 2 down vote accepted

Ah, found it:

for /f %%x in ('dir *.exe /B /O:-D') do set NEWEST_EXE & goto DONE

share|improve this answer

There's almost certainly a more sane way to do this in PowerShell.
But if it has to be cmd.exe:

FOR /f %f IN ('DIR /b /od *.exe') DO @SET last=%f

ECHO %last%

DIR /b lists just the filename, like ls -1 would do; the /od is of course date order.
So the last iteration sets the name of the last file in the list.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.