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.
I tried to use this script:
@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%"
)
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.
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:
The above command will copy only new files or files that have been modified since the last backup.
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.