Copying files based on date modified in batch file

ε祈祈猫儿з 提交于 2019-12-04 02:33:15

问题


I am trying to copy files from one directory to another using a DOS batch script. The files I want to copy are the 4 or 3 latest files. That number will be static, but yet to be determined. Is there anyway to copy based on date modified?

Thank you


回答1:


You could:

1) have the dir command sort files in the descending order of date modified;

2) use the output of the dir command in a `for loop to copy the corresponding files;

3) count to 3 (or 4) in the for loop to limit the number of files copied.

@ECHO OFF
SET "srcdir=D:\Source"
SET "tgtdir=D:\Target"
SET /A topcnt=3
SET /A cnt=0
FOR /F "tokens=*" %%F IN ('DIR /A-D /OD /TW /B "%srcdir%"') DO (
  SET /A cnt+=1
  SETLOCAL EnableDelayedExpansion
  IF !cnt! GTR !topcnt! (ENDLOCAL & GOTO :EOF)
  ENDLOCAL
  COPY "%srcdir%\%%F" "%tgtdir%"
)


来源:https://stackoverflow.com/questions/7391835/copying-files-based-on-date-modified-in-batch-file

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