Use VBS to delete the backup file created the day before _vbs

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! A program creates a file that has a name similar to the following every day: Backup_20050607.bak. How can I delete a file from the day before?
--JC
For:
Hello, JC. Well, you can call a script to automatically delete any files that were generated the day before. Man, just a few weeks ago we used a script like this!
Yes, we still have impressions. But the past cannot be revisited.
In addition, we have to write a column here. You need a script that deletes the file name such as Backup_20050607.bak (where, 20050607 means the day before, in this case is June 7, 2005), right? All you need to do is use:
Copy Code code 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 of this is the build file name; with the filename, deleting the file is trivial. So, how do we build that file name?
Yes, the portion of the file name that changes every day is the part that represents the date that the backup file was generated. In other words, all we have to do is build this part of the file name, and other parts can be hard-coded to join. So we need to start with the date of the day before, which can be done by subtracting the current date by one and then storing it in a variable named Dtmyesterday:
Dtmyesterday = Date-1
Next, use the year function to obtain a four-digit years value (for example, 2005) from Dtmyesterday, which is stored in the variable stryear. Then use the Month function to get the month value from Dtmyesterday.
Note, however, that additional coding is required for the month value (also for dates). Why, then? Yes, assuming we're dealing with the June. At this point, the Month function returns the value 6. There's nothing wrong with that, except that we've allocated two digits to the month in our filename, and we need to list the months in 06. Therefore, we have to determine whether the resulting month is a number or a two-digit number. If it is a numeric month, you must precede the number with a 0. We do this just like the following:
If Len (strmonth) = 1 Then
Strmonth = "0" & Strmonth
End If
This piece of code is really at a glance. The Len (length) function tells us how many digits are in the strmonth variable. If you have only 1 digits, add the leading 0:
Strmonth = "0" & Strmonth
If you have any value other than 1, just leave it as it is.
After you get the month value, repeat the same procedure with function day so that you can retrieve the days section of the date. The value will be stored in the cleverly named variable Strday.
This provides all the data we need to build the file name. To do this, you first use the following line of code to bring the dates together (get a string similar to 20050607):
Stryesterday = stryear & Strmonth & Strday
Then, just append the rest of the path information (in this case, suppose the file is stored under the folder C:\Backups):
strFileName = "C:\Backups\backup_" & Stryesterday & ". Bak"
As you can see, we simply combine the string C:\Backups\backup_, the date built (20050607), and. Bak. Combining them together will result in similar results as follows:
C:\Backups\backup_20050607.bak
It's cool.
We then create a FileSystemObject instance using the following two lines of code and delete the file:
Set objFSO = CreateObject ("Scripting.FileSystemObject")
objFSO.DeleteFile (strFileName)
Because of the use of FileSystemObject, this script can only run on the local machine because of the use of FileSystemObject. 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? Yes, FileSystemObject can find and delete files in less than a second, while WMI takes a little longer. We decided to start with speed and efficiency.
But as we said, if the file is on a remote computer, speed and efficiency do not bring you much benefit. Fortunately, in this case, you can use WMI scripts to delete files. The following script, which is not discussed today, will remove the backup file from the remote computer named atl-fs-01:
Copy Code code 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 objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Colfiles = objWMIService.ExecQuery _
("Select * from Cim_datafile where Name = '" & strFileName & "")
For each objfile in Colfiles
Objfile.delete
Next
Like this, yesterday is gone, we can all pretend that it never happened. Isn't that right, boss?
Note: Can't you save all this trouble by simply removing all files created/modified that are equal to the day before? Yes, as long as there is never any other file in the folder that has the same date that might have been accidentally deleted. These practices appear to be the safest of all, taking into account various factors.

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.