问题
I have a directory of many files in Windows Vista. I'd like in a batch script to be able to pick the newest file and copy it to another location. Any ideas how I do that?
回答1:
You can use the for command to invoke a directory listing that is sorted by date, and use it to set an environment variable, if you set the same variable to each file, then it will end up being set to the latest file.
put this into a batch file:
for /F "delims=" %%I in ('dir /b /a-d /od') do set LATEST=%%I
echo "%LATEST%"
then you can use the move command to move that file to wherever you want it.
move "%LATEST%" wherever
EDIT 10-Feb-2011: fixed to handle filenames with spaces in them. The fix is to use "delims=" to disable tokeninzing on space. Thanks to Dave Potts for the fix.
来源:https://stackoverflow.com/questions/2469234/how-do-i-copy-the-newest-file-in-a-directory-somewhere-else-from-the-command-lin