简体   繁体   中英

Batch file to copy the newest file created (not modified)

I know there are a fair amount of questions about batch files copying the latest file. But I can't seem to find one that addresses my requirement.

I have a batch file...

(@echo off & setlocal enableextensions enabledelayedexpansion) & (set myfolder_=Z:\myfolder1) & (for /f "delims=" %%f in ('dir /o:d /a:-d /b "!myfolder_!\*.mp3"') do (set latest_=!myfolder_!\%%~nxf)) & (if defined latest_ (echo rem latest file "!latest_!" & xcopy "!latest_!" "Z:\myfolder2" /V /D /Y) else (echo No designated files found in "!myfolder_!")) & (Pause) & (endlocal & goto :EOF)

It needs to copy across the latest file, by created date, but currently it seems to sort based on the modified date.

For example, if I modify the mp3 tag on an old file, this script appears to select and copy that file.

Note delayed expansion is on because I have to run this as a single line.

Thanks in advance:)

Include /TC in dir command when parsing with for /F :

(@echo off & setlocal enableextensions enabledelayedexpansion) & (set myfolder_=Z:\myfolder1) & (for /f "delims=" %%f in ('dir /TC /OD /A-D /B "!myfolder_!\*.mp3"') do (set latest_=!myfolder_!\%%~nxf)) & (if defined latest_ (echo rem latest file "!latest_!" & xcopy "!latest_!" "Z:\myfolder2" /V /D /Y) else (echo No designated files found in "!myfolder_!")) & (Pause) & (endlocal & goto :EOF)

should work.

/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written

From dir /? second help page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM