Automatically backup files and folders with batch files, and automatically delete files n days ago


The following is the batch processing of the backup, which is added to the “scheduled task” to set the time to run automatically

Copy codeThe code is as follows:
@echo off
REM format date
The date of REM date is “2006-02-22 Wednesday”. It can’t be used directly, so you should format it first
REM becomes what we want. Date: ~ 0,4 means to intercept 4 characters from 0
set d=%date:~0,4%%date:~5,2%%date:~8,2%
REM sets the path of the compression program, which is packaged with rar.exe of WinRAR
set path=C:\Program Files\WinRAR
REM set the directory to be backed up
set srcDir=D:\databasc
REM set the directory of backup files
set dstDir=E:\temp\backup
REM sets the prefix of the backup file. Currently, it is temp and the prefix is backup
set webPrefix=
REM start backup if file does not exist
if not exist %dstDir%%webPrefix%%d%.rar start Rar a -r %dstDir%%webPrefix%%d%.rar %srcDir%
@echo on

The following is the batch processing for deleting the backup files n days ago. Note: under Windows 2003, for example, XP, because there is no forfiles.exe, it is not tested to copy the 2003 files to the system32 directory of XP
Delete the file with the last modified date 10 days ago in the backup directory of Disk C
forfiles /p “c:\backup” /d -10 /c “cmd /c echo deleting @file … && del /f @path”
The following is a batch process for copying files to other locations one day ago
To copy the files with the last modification date greater than or equal to July 1, 2007 under the root directory of Disk C to the root directory of disk D
forfiles /p “c:\” /d “2007-7-1” /c “cmd /c copy @path d:\”
Built in command line file of Windows Server 2003, suitable for systems above XP
forfiles /m *.bkf /d -28 /c “cmd /c del @file /f”
Delete the BKF file with the extension 28 days ago in the current directory (based on the current system time)
Forfiles automatically delete files 7 days ago delete
2008-09-12 15:01
Try forfiles.exe.
forfiles /p “d:\test” /s /m *.* /d -7 /c “cmd /c del @path”
Make batch file. Bat file and forfiles.exe are in the same directory. Create a shortcut to bat file at startup.
d: \ test to the directory path you want. Date refers to the date of modification.
Attach forfiles.exe and help.
Delete all empty directories (for example, delete the D: \ test directory)
dir /ad/b/s d:\test\ |sort /r >d:\kill.txt
For /f “tokens=*” %%i in (d:\kill.txt) DO rd “%%i”
del d:\kill.txt
Copy the following to the bat file.
@echo off
forfiles /p “d:\test” /s /m *.* /d -7 /c “cmd /c del @path”
dir /ad/b/s d:\test\ |sort /r >d:\kill.txt
For /f “tokens=*” %%i in (d:\kill.txt) DO rd “%%i”
del d:\kill.txt
Delete expired files first, and then delete all empty directories
If the operating system is Windows Server 2003, it is easy to do, because it has a forfiles command to find files that meet the specified conditions. Here is the usage of this command.
Forfiles / P < target directory name > / d < days > / C < executed command >
See what the three parameters listed are useful for:
/P specifies the directory in which to find files. The default is the current working directory.
/D specifies a date or number of days to compare whether the last modified date of the file meets the criteria.
/C for each file found.
For example 1. To copy the file with the last modification date greater than or equal to July 1, 2007 under the root directory of Disk C to the root directory of disk D:
forfiles /p “c:\” /d “2007-7-1” /c “cmd /c copy @path d:\”
For example 2. Delete the file whose last modification date is 10 days ago in the backup directory of Disk C:
forfiles /p “c:\backup” /d -10 /c “cmd /c echo deleting @file … && del /f @path”
First look at the code to delete the file
Forfiles / P contains the full path of the file to be deleted (for example: F: \ logfiles) / m *. Log – D – 7 / C “CMD / C del / F @ path”
Explain the relevant parameters and commands
/P < Path >: Specifies the location to start searching for files. If not specified, it defaults to the current directory.
/M < searchmask >: the wildcard used for file search, such as “*. Log” in the code, is all log files. Of course, you can also specify all log files that start with “mamee” *. Log “, such as” mamee “*. Log”. If this parameter is not specified, it defaults to “*. *”.
/D [{+ | -}] [{< date > | < days >}]: specify the last modification time of the file you want to select. In the above, “/ D-7” indicates all files based on the current day and 7 days ago. Of course, you can also specify a specific time here, for example: “/ D – 08 / 18 / 2009” all documents earlier than August 18, 2009. Note that the specified time must be in the format mm / DD / yyyy.
/C executes the specified command for all files. The command body must be in double quotation marks (“), and the default is” CMD / C echo @ file “. “CMD / C del / F @ path” is used to delete the specified file. (here @ file and @ path are variables, which will be explained later.)
Let’s talk about the parameters used above:
@Path: indicates the full path of the file.
@File: indicates the file name.
Click here for details of other parameters.
Next let’s look at the operation of deleting folders. If you read the above introduction, I believe you can understand this command as soon as you read it.
Forfiles / P contains the path of the folder (e.g. F: \) / M folder name (e.g. logfiles) – d 0 / C “CMD / C if @ isdir = = true rd / S / Q @ path”
Note that the “path containing folder” here cannot contain the folder to be deleted. As the above code indicates, look for the file or folder named logfiles in the f disk (you can’t specify the search folder, but we made a judgment when deleting).
Another is that a new parameter “@ isdir” appears here, which is used to determine whether the current file type is “folder type”. If it is, it is true; otherwise, it is false.
I believe that you will understand, and finally save the code as a batch file, and then add the scheduled task to execute on a regular basis.
After reading the above example, I think it’s easy to delete the old files under Windows Server 2003.
But if the operating system is Windows 2000 / XP, it will be more troublesome, because they do not have the command for files, so they can only be realized by writing batch by themselves.
Here is what I wrote about the batch file:

Copy codeThe code is as follows:
@echo off
rem ******************************
REM * batch process for deleting file directories by time*
rem ******************************
REM set the path of the temporary directory
set tempDir=%tmp%\remove_%date:~0,10%
if not exist %tempDir% md %tempDir%
REM sets the path of the script file to process the date
set scriptFile=%tempDir%\get_date.vbs
REM gets the number of days to keep
set days=%~1
if “%days%” == “” goto printUsage
REM get the path of the target directory
set dirPath=%~2
if “%dirPath%” == “” set dirPath=.
REM gets the file form to operate on
set fileSpec=%~3
if “%fileSpec%” == “” set fileSpec=*.*
REM generates a script file that calculates the date and gets the deadline for deletion
echo d=date()-%1 > %scriptFile%
echo s=right(“0000” ^& year(d),4) ^& “-” ^& right(“00” ^& month(d),2) ^& “-” ^& right(“00” ^& day(d),2) >> %scriptFile%
echo wscript.echo s >> %scriptFile%
for /f %%i in (‘cscript /nologo %scriptFile%’) do set lastDate=%%i
REM handles every object in the target directory
for /f “tokens=1,2,3* delims=<> ” %%i in (‘dir “%dirPath%\%fileSpec%” /a /-c /tc’) do call :proc “%%i” “%%j” “%%k” “%%l”
goto :done
REM processing of objects in the target directory
REM gets the creation date of the object and determines whether it is a valid format
set fileDate=%~1
echo %fileDate% | findstr “[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]” > nul
if errorlevel 1 goto end
REM gets the type of the object
set fileType=%~3
if “%fileType%” == “” goto end
REM gets the name of the object
set fileName=%~4
if “%fileName%” == “” goto end
if “%fileName%” == “.” goto end
if “%fileName%” == “..” goto end
If “% filename%” = = “byte” goto end
If “% filename%” = = “available bytes” goto end
REM determines whether the object date is less than or equal to the deletion deadline
if “%fileDate:~0,10%” leq “%lastDate%” (
echo deleting “%fileName%” …
if “%fileType%” == “DIR” ( rd /s /q “%dirPath%\%fileName%” ) else ( del /q /f “%dirPath%\%fileName%” )
goto end
echo An error occurred during backuping.
rd /s /q %tempDir%
goto end
echo Usage: %0 ^<Days^> [Work directory] [Target file specification (can include wildcards)]
goto end

It mainly uses the script function of windows to calculate the deadline for deleting files, and then for plus the dir command to extract the file date for judgment.
For files and for can be found in windows help and support.