Use vbs to delete backup files created the previous day

Source: Internet
Author: User

Q:
Hello, script expert! Every day, a program creates a file with the following name: backup_20050607.bak. How can I delete files from the previous day?
-- JC
A:
Hi, JC. Well, you can call a script to automatically delete any files generated the previous day. Dude, we used a script like this a few weeks ago!
Yes, we are still impressed. However, we cannot repeat the past.
In addition, we need to write a column here. You need a script to delete the file named backup_20050607.bak (wherein, 20050607 indicates the previous day, in this example, It is July 22, June 7, 2005), right? All you need to do is to use: Copy codeThe Code is as follows: dtmYesterday = Date-1
StrYear = Year (dtmYesterday)
StrMonth = Month (dtmYesterday)
If Len (strMonth) = 1 Then
StrMonth = "0" & strMonth
End If
StrDay = Day (dtmYesterday)
If Len (strDay) = 1 Then
StrDay = "0" & strDay
End If
StrYesterday = strYear & strMonth & strDay
StrFileName = "C: \ Backups \ backup _" & strYesterday & ". bak"
Set objFSO = CreateObject ("Scripting. FileSystemObject ")
ObjFSO. DeleteFile (strFileName)

As you may have expected, the most tricky part here is the build file name. Deleting a file with a file name is a trivial matter. So how should we build the file name?
Yes, the part in the file name that changes every day is the part that represents the date when the backup file was generated. That is to say, all we need to do is to build the part of the file name, and other parts can be hard-coded. Therefore, we need to start from the date of the previous day, which can be done by subtracting one from the current date, and then saving the value to a variable named dtmYesterday:
DtmYesterday = Date-1
Next, use the Year function to obtain the Year value (for example, 2005) of four digits from dtmYesterday. The value is stored in the variable strYear. Then use the Month function to obtain the Month value from dtmYesterday.
However, please note that the month value must be further encoded (for date ). Why? Yes. Assume we are dealing with February. In this case, the Month function returns 6. This is no problem, except that two digits are allocated for the month in our file name; the month needs to be listed in 06 format. Therefore, we must determine whether the given month is a digit or a double digit. If the month is a digit, add zero before the digit. We do this as follows:
If Len (strMonth) = 1 Then
StrMonth = "0" & strMonth
End If
This code is really clear at a glance. The Len (length) function tells us the number of digits in the strMonth variable. If there is only one digit, add the leading 0:
StrMonth = "0" & strMonth
If the length is any value other than 1, you only need to keep it as it is.
After the month value is obtained, use the function Day to repeat the same process to retrieve the Day part of the date. This value will be stored in the cleverly named variable strDay.
This provides all the data we need to build a file name. To do this, first use the following line of code to combine the year, month, and day (get a string similar to 20050607 ):
StrYesterday = strYear & strMonth & strDay
Then, you only need to append the remaining path information (in this example, assume that the file is stored in the C: \ Backups folder ):
StrFileName = "C: \ Backups \ backup _" & strYesterday & ". bak"
As you can see, we only need to combine the string C: \ Backups \ backup _, the constructed date (20050607), and. bak. After they are combined, a result similar to the following will be obtained:
C: \ Backups \ backup_20050607.bak
It's cool.
Then, we will use the following two lines of code to create a FileSystemObject instance and delete the file:
Set objFSO = CreateObject ("Scripting. FileSystemObject ")
ObjFSO. DeleteFile (strFileName)
Because FileSystemObject is used and FileSystemObject is used, this script can only be run on a local machine. But what if the backup file is on a remote computer? No problem. You can use WMI to locate and delete the file. So why not use WMI first? In this case, FileSystemObject can be found and deleted in less than one second, while WMI takes a little longer. We decided to take speed and efficiency first.
But as we said, if the file is on a remote computer, the speed and efficiency will not bring you too much benefits. Fortunately, in this case, you can use the WMI script to delete files. The following script, which is not discussed today, will delete the backup file from a remote computer named atl-fs-01:Copy codeThe Code is as follows: dtmYesterday = Date-1
StrYear = Year (dtmYesterday)
StrMonth = Month (dtmYesterday)
If Len (strMonth) = 1 Then
StrMonth = "0" & strMonth
End If
StrDay = Day (dtmYesterday)
If Len (strDay) = 1 Then
StrDay = "0" & strDay
End If
StrYesterday = strYear & strMonth & strDay
StrFileName = "C :\\ Backups \ backup _" & strYesterday & ". bak"
StrComputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colFiles = obw.miservice. ExecQuery _
("Select * from CIM_DataFile where Name = '" & strFileName &"'")
For Each objFile in colFiles
ObjFile. Delete
Next

Just like this, yesterday has passed, and we can all pretend that it has never happened. Right, boss?
Note: Isn't it possible to simply delete all files whose creation/modification date is the same as the previous day? Yes, as long as the folder will never have any other files with the same date and may be accidentally deleted. Taking into account various factors, the above practices seem to be the safest.

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.