How to get the most recent file using a batch file?

Viewed 72119

I have a list of zip files with date and time appended like yyyymmdd_hhmmss_Demos.zip. Now how to get the most recently added zip file in the source dir. I need to copy this file in the target using copy command.

I found some info about forfiles, but do not have an idea on how to get it done for seconds.

4 Answers
set Path="D:\hello\abc\old"
for /f "tokens=*" %%a in ('dir /A:-D /B /O:-D /S %Path%') do set NEW=%%a&& goto:n 
:n
echo %NEW%

Below snippet will extract the date and customize as per your needs

for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
     set dow=%%i
     set month=%%j
     set day=%%k
     set year=%%l
)
:: Pad digits with leading zeros e.g Sample_01-01-21.csv
    set yy=%year:~-2%
    set datestr=%day%-%month%-%yy%

Alternate way:

set datestr=%date:~0,2%-%date:~3,2%-%date:~6,2%
Related