问题描述
我需要为目录创建一个备份批处理脚本。它将每10分钟更新一次。我希望仅更新已添加到目录中的文件或在上次备份后进行修改。
I need to create a backup batch script for a directory. It will update every 10 minutes. I would like it to only update files that have been added to the directory or modified after the last backup.
我尝试使用这个脚本:
@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%"
)
我所遇到的问题是它仅在批处理文件所在的目录中有效,这将返回最近的三个文件包括批处理文件本身。另外,复制功能不起作用。程序没有连接srcdir与文件扩展名,因此程序无法确定要复制的文件。请指教。
The problems I have is that it only works in the directory that the batch file is in, which will return the most recent three files including the batch file itself. Additionally, the copy function is not working. The program is not connecting the srcdir with the file extension, thus the program cannot determine what file to copy. Please advise.
推荐答案
我不明白您的代码与您在问题第一段所述目标相关。如果你的目标真的像你说的那么简单,那么你不需要一个批处理文件。您只需要安排以下命令每10分钟运行一次:
I don't understand how your code relates to your stated goal in the first paragraph of your question. If your goal is really as simple as you stated, then you don't need a batch file. You just need to schedule the following command to run every 10 minutes:
xcopy /d D:\Source\* d:\Target
上述命令将仅复制自上次修改以来的新文件或文件备份。
The above command will copy only new files or files that have been modified since the last backup.
如果您的备份要求变得更加复杂,那么您可能应该切换到ROBOCOPY。它有丰富的选择,可能会满足您的需求。仍然没有批量要求。
If your backup requirements become more complicated then you probably should switch over to ROBOCOPY. It has a wealth of options that would probably meet your needs. Still no batch required.
这篇关于使用批处理文件更新和备份目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!