pharaon450
pharaon450

Reputation: 523

Show all files in directory and select the latest creation date one with windows command line

is there a way with windows command line to list all the files from a specific directory and then select the name of the file that has the newest creation date.

Thank you

Upvotes: 1

Views: 1440

Answers (1)

Darth Continent
Darth Continent

Reputation: 2319

This iterates through a directory listing and sets an environment variable to each file in order by date; the last one set would be the newest file:

for /F "delims=" %%I in ('dir /b /a-d /od') do set LATEST=%%I
echo "%LATEST%"

Then you could copy the file like so:

copy "%LATEST%" destination

Based on the solution found here.

EDIT: I also got the %%I was unexpected at this time error when executing this directly through the command prompt, but it worked fine from within a batch file. You could create a batch file with the above and it should work.

Upvotes: 2

Related Questions