根据批处理文件中修改的日期复制文件 [英] Copying files based on date modified in batch file

查看:23
本文介绍了根据批处理文件中修改的日期复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 DOS 批处理脚本将文件从一个目录复制到另一个目录.我要复制的文件是 4 或 3 个最新文件.这个数字将是静态的,但尚未确定.无论如何要根据修改日期复制吗?

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?

谢谢

推荐答案

你可以:

1) 让 dir 命令按照修改日期的降序对文件进行排序;

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

2)在`for循环中使用dir命令的输出来复制相应的文件;

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

3) 在 for 循环中计数为 3(或 4),以限制复制的文件数.

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%"
)

这篇关于根据批处理文件中修改的日期复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆