How do I copy the newest file in a directory somewhere else from the command line in windows vista

蓝咒 提交于 2019-12-11 13:59:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!