Automatically backs up files and folders with batch files and automatically deletes files n days ago _dos/bat

Source: Internet
Author: User
Tags echo d goto rar
The next is the batch of the backup, added to the scheduled task, set the time to run automatically
Copy Code code as follows:

@echo off
REM Format Date
REM date is "2006-02-22 Wednesday" and cannot be used directly, so you should format it first.
REM becomes what we want. date:~0,4 means to intercept 4 characters starting from 0.
Set d=%date:~0,4%%date:~5,2%%date:~8,2%
REM Sets the compression program path, which is packaged with WinRAR Rar.exe.
Set Path=c:\program Files\winrar
REM Set the directory to back up
Set SRCDIR=D:\DATABASC
REM Set the directory where the backup files are located
Set Dstdir=e:\temp\backup
REM Sets the prefix for the backup file, currently temp, with the prefix backup
Set webprefix=
REM Start a backup If the 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 deletion of the batch of backup files N days ago, note: Under Windows2003, such as XP because there is no forfiles.exe, no test copies of 2003 of files to the XP system32 directory
Delete files with the last modified date of the C disk backup directory 10 days ago
=================================================================================
forfiles/p "C:\Backup"/d-10/c "cmd/c echo deleting @file ... && del/f @path"
=================================================================================
The following is a batch that replicates files to another location before a day
Copy the file in the C-packing directory to the D-packing directory with the last modification date greater than or equal to July 1, 2007
=================================================================================
forfiles/p "C:\"/"2007-7-1"/C "cmd/c copy @path d:\"
Windows Server 2003 built-in command-line files, suitable for systems above XP
Example:
forfiles/m *.bkf/d -28/c "cmd/c del @file/F"
Delete the current directory 28 days before the extension is BKF file (based on current system time)
Forfiles automatically deletes file delete 7 days ago
2008-09-12 15:01
Try it with Forfiles.exe.
forfiles/p "D:\test"/s/m *.*/d-7/C "cmd/c del @path"
Make a batch file, bat file with Forfiles.exe the same directory, build a bat file shortcut in the start.
D:\test change to the directory path you want. Date refers to the date of the modification.
Attach Forfiles.exe and help.
Delete all empty directories (to delete the D:\test directory for example)
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
First delete the expired file, and then delete all the empty directory
This is fine if the operating system is Windows Server 2003, because it has a forfiles command to find files that meet the specified criteria, and the following is the use of this command.
forfiles/p < target directory name >/d < days >/C < execute command >
See what the three parameters listed are useful:
/p Specifies the directory in which to look for files, and 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 commands that are executed for each file found.
Example 1. To copy the file in the C-packing directory to the D-packing directory with the last modification date greater than or equal to July 1, 2007:
forfiles/p "C:\"/"2007-7-1"/C "cmd/c copy @path d:\"
Example 2. Delete the file that was last modified 10 days before the date of the backup directory in the C disk:
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 to delete files (such as: F:\Logfiles)/M *.log-d -7/c "cmd/c del/f @path"
Explain the relevant parameters and commands
/P <Path>: Specifies where to start searching for files, and defaults to the current directory if unspecified.
/M <SearchMask>: The wildcard characters used for file lookups, such as "*.log" in code, are all log files, and of course you can specify all log files that start with manmee such as "Manmee_*.log". If you do not specify this parameter, the default is "*.*".
/d [{+|-}][{<date>|<days>}]: Specifies the last modification time of the file you want to select, and "/d-7" is used above to indicate all files based on the day, 7 days old. Of course, you can also specify a specific time, such as "/d-08/18/2009" for 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 quotes ("), and the default is" cmd/c echo @file ". The "cmd/c del/f @path" is used above to delete the specified file. (The @file and @path here are variables, as explained below.) )
Here are the parameters used in the above:
@PATH: Represents the full path of a file.
@File: Represents the file name.
More about the other parameters click here.
Next we take a look at the operation of the Delete folder, if you read the above introduction, I believe that the command you can read.
FORFILES/P contains the path to the folder (such as: f:\)/M folder name (for example: LogFiles)-D 0/c "cmd/c if @ISDIR = = True rd/s/q @path"
Note that the "path to include folder" Here cannot contain the folder you want to delete, as shown in the above code to look for a file or folder named LogFiles in the F disk (you cannot specify a lookup folder, but we made a decision when deleting it).
And there is a new parameter "@ISDIR" he used to determine whether the current file type is a folder type, or False if it is true.
Believe that everyone will understand, and finally save the code as a batch file, and then join the scheduled task to execute on a regular basis.
Looking at the example above, it's easy to delete old files under Windows Server 2003.
But if the operating system is Windows 2000/xp it is troublesome because they do not have the forfiles command, they can write batch processing on their own.
The following are the contents of the batch file I wrote:
Copy Code code as follows:

@echo off
REM ******************************
REM * Batch deletion of file directories by Time *
REM ******************************
REM Set the path of the temp directory
Set tempdir=%tmp%\remove_%date:~0,10%
If not exist%tempdir% MD%tempdir%
REM Sets the path of a script file that processes dates
Set Scriptfile=%tempdir%\get_date.vbs
REM Gets the number of days to keep
Set Days=%~1
If "%days%" = "" Goto printusage
REM Gets the path to the target directory
Set dirpath=%~2
If "%dirpath%" = "" Set dirpath=.
REM Gets the file form to manipulate
Set Filespec=%~3
If "%filespec%" = "" Set Filespec=*.*
REM generates a script file that calculates the date and gets the expiration date for the deletion
Echo d=date ()-%1 >%scriptfile%
echo s=right ("0000" ^& Year (d), 4) ^& "-" ^& Right ("^& month (d), 2) ^&"-"^& Right (" "^& Day (d), 2 >>%scriptfile%
Echo WScript.Echo S >>%scriptfile%
for/f%%i in (' Cscript/nologo%scriptfile% ') do set lastdate=%%i
REM processes each object in the target directory
for/f "tokens=1,2,3* delims=<>"%%i in (' dir '%dirpath%\%filespec% '/A/-c/tc ') do call:p Roc "%%i" "%%j" "%%k" " %%l "
Goto:D One
REM processes the objects in the target directory
:p Roc
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 date the deletion is due
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
: Error
Echo an error occurred during backuping.
:d One
RD/S/q%tempdir%
Goto END
:p Rintusage
echo Usage:%0 ^<days^> [Work directory] [Target file specification (can include wildcards)]
Goto END
: End

The main use of Windows scripting features to calculate the deadline to delete files, and then for the dir command to extract the date of the file to judge.
For more information about forfiles and for, you can find it in Windows Help and support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.