Search code examples
batch-fileforfiles

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


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.


Solution

  • You can use

    pushd D:\a
    for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
    copy "%newest%" D:\b
    popd