Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use batch files to automatically back up files and folders and automatically delete files from n days ago

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use batch files to automatically back up files and folders and automatically delete files from n days ago. what is introduced in this article is very detailed and has a certain reference value. Interested friends must finish reading it!

Next is the batch processing of the backup, which is added to the "scheduled task" and set the time to run automatically.

The code is as follows:

@ echo off

Rem format date

The date of rem date is "2006-02-22 Wednesday", so it cannot be used directly, so it should be formatted first.

Rem becomes what we want. Date:~0,4 means to intercept 4 characters starting at 0.

Set dongs% dateGroups 0Magi 4% dateRod 5pas 2% daterids 8pr 2%

Rem sets the compressed program path, which is packaged with WINRAR's rar.exe.

Set path=C:\ Program Files\ WinRAR

Rem sets the directory to be backed up

Set srcDir=D:\ databasc

Rem sets the directory where the backup files are located

Set dstDir=E:\ temp\ backup

Rem sets the prefix for backup files, which is currently temp and prefix backup.

Set webPrefix=

Rem if the file does not exist, start backup

If not exist% dstDir%%webPrefix%%d%.rar start Rar a-r% dstDir%%webPrefix%%d%.rar% srcDir%

@ echo on

The following is the batch process for deleting files backed up N days ago. Note: under windows2003, such as XP, because there is no forfiles.exe, you did not test to copy 2003 of the files to the system32 directory of XP.

Delete files under the backup directory of disk C that were last modified 10 days ago

=

Forfiles / p "c:\ backup" / d-10 / c "cmd / c echo deleting @ file... & & del / f @ path"

=

The following is a batch process for copying files from a day ago to another location

To copy the files whose last modification date is greater than or equal to July 1, 2007 in the C root directory to the D disk root directory

=

Forfiles / p "c:\" / d "2007-7-1" / c "cmd / c copy @ path d:\"

Windows Server 2003 built-in command line files, suitable for XP and above systems

Example:

Forfiles / m * .bkf / d-28 / c "cmd / c del @ file / f"

Delete the 28-day extension bkf file under the current directory (based on the current system time)

Forfiles automatically deletes the file delete 7 days ago

2008-09-12 15:01

Try using forfiles.exe.

Forfiles / p "d:\ test" / s / m *. * / d-7 / c "cmd / c del @ path"

Make a batch file, bat file and forfiles.exe in the same directory, create a shortcut to bat file in startup.

D:\ test change to the directory path you want. Date refers to the date of modification.

Attach forfiles.exe and help.

Delete all empty directories (take deleting the d:\ test directory as an 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 into 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

It would be easy if the operating system is Windows Server 2003, because it has a forfiles command that can find files that meet the specified criteria. Here is the use of this command.

Forfiles / p / d / c

See what the usefulness of the three parameters listed are:

/ p specifies the directory in which to look for files, and the default is the current working directory.

/ d specify a date or number of days to compare whether the last modified date of the file meets the criteria.

/ c the command executed for each file found.

Example 1. To copy files in the C root directory with a last modification date greater than or equal to July 1, 2007 to the D root directory:

Forfiles / p "c:\" / d "2007-7-1" / c "cmd / c copy @ path d:\"

Example 2. Delete the files under the backup directory of disk C that were last modified 10 days ago:

Forfiles / p "c:\ backup" / d-10 / c "cmd / c echo deleting @ file... & & del / f @ path"

First look at the code that deletes the file.

Forfiles / p contains the full path of the file to be deleted (e.g. F:\ Logfiles) / m * .log-d-7 / c "cmd / c del / f @ path"

Explain the relevant parameters and commands

/ p: specify the location where to start the search for files, or the current directory by default if not specified.

/ m: the wildcard characters used in file lookup, such as "* .log" in the code, are all log files. Of course, you can also specify all log files that start with manmee, such as "manmee_*.log". If you do not specify this parameter, it defaults to "*. *".

/ d [{+ | -}] [{|}]: specify the last modification time of the file you want to select. "/ d-7" is used above to indicate all files based on the current day and 7 days ago. Of course, you can also specify a specific time here, such as "/ d-08swap 18max 2009" for all files that are earlier than August 18, 2009. Note that the specified time must be in the format "MM/DD/YYYY".

/ c execute the specified command on all files in double quotation marks ("). The default is" cmd / c echo @ file ". What is used above is" cmd / c del / f @ path "to delete the specified file. (@ file and @ path here are variables, which will be explained below.)

Let's talk about the parameters used above:

PATH: represents the full path to the file.

File: indicates the name of the file.

Click here for a detailed description of the other parameters.

Next, let's take a look at the operation of deleting a folder. If you read the above introduction, I believe you can understand this command as soon as you read it.

Forfiles / p contains the path to the folder (e.g. F:\) / m folder name (e.g. LogFiles)-d 0 / c "cmd / c if @ ISDIR = = true rd / sbank Q @ path"

Note that the "path to the containing folder" here cannot contain the folder to be deleted, as shown in the above code, look for a file or folder named LogFiles on the F disk (you cannot specify the lookup folder, but we made a judgment when deleting it).

Also, there is a new parameter "@ ISDIR" that is used to determine whether the current file type is a "folder type". If so, it is true or false.

I believe that you will understand, and finally save the code as a batch file, and then join the scheduled task to execute on a regular basis.

After reading the example above, I think it's easy to delete old files under Windows Server 2003.

But it's troublesome if the operating system is Windows 2000/XP, because they don't have forfiles commands, so they have to write batches on their own.

Here are the contents of the batch file I wrote:

The code is as follows:

@ echo off

Rem * *

Rem * batch process of deleting file directories by time *

Rem * *

Rem sets the path to the temporary directory

Set tempDir=%tmp%\ remove_%date:~0,10%

If not exist tempDir% md tempDir%

Rem sets the path to the script file for the processing date

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 of the target directory

Set dirPath=%~2

If "% dirPath%" = "" set dirPath=.

Rem gets the file form to be operated

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

The process of dealing with objects in the target directory by rem

: proc

Rem gets the creation date of the object and determines whether it is in a valid format

Set fileDate=%~1

Echo% fileDate% | findstr "[0-9] [0-9] [0-9] [0-9]-[0-9] [0-9]" > nul

If errorlevel 1 goto end

Rem gets the type of 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 due date.

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.

: done

Rd / s / Q% tempDir%

Goto end

: printUsage

Echo Usage:% 0 ^ [Work directory] [Target file specification (can include wildcards)]

Goto end

: end

Mainly use the script function of Windows to calculate the due date of the file to be deleted, and then for plus the dir command to extract the date of the file to judge.

The above is all the contents of the article "how to use batch files to automatically back up files and folders and automatically delete files from n days ago". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report