How to automate deleting a folder that satisfies a certain condition

Source: Internet
Author: User

Requirements: Because the company has a long time to back up files, all need to know how to delete the backup files, the daily operation is cumbersome and waste time, so now want to design a command, can be set in the schedule, the daily run the command, delete the n days before the backup data, and production delete log.

Below the order to write the younger brother and everyone to share under:

1, first in disk drive C under the new two folders: C:\Script and C:\TestFolder, and then C:\TestFolder under a few new folders, C:\Script under the following several files: 1, DELETEBACKUPFOLDER.CMD;2), deletebackupfolder.vbs;3), Log.txt

2. The Deletebackupfolder.vbs command has the following contents:

On Error Resume Next

Dim Strdate,objfld,objsflds,files

' Save the database backup file path

Strfolder = "C:\TestFolder"

StrFolder1 = "C:\Script"

Set objFSO = CreateObject ("Scripting.FileSystemObject")

Set objfld = objFSO. GetFolder (Strfolder)

Set objsflds = objfld. Subfolders

' Used to write a text file and generate a delete database backup report

Const ForAppending = 8

' Create an empty TXT file under scripts: Log.txt

Set objfile = objFSO. OpenTextFile (StrFolder1 & "\log.txt", ForAppending)

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/database/storage/

Objfile.write "================================================================" & VBCRLF & VBCRLF

Objfile.write "Database Documentation Report" & VBCRLF

Objfile.write "Date:" & FormatDateTime (Now (), 1) & "" & VBCRLF

Objfile.write "Time:" & FormatDateTime (Now (), 3) & "" & VBCRLF & VBCRLF

Objfile.write "================================================================" & VBCRLF

' Enumerate backup file directories

For each objsfld in Objsflds

' Get the filename of the file you want to delete

A=objsfld. Name

' Strdate = Split (objsflds. Name, "")

' Check if the backup is N days ago

' If DateDiff ("D", strdate (0), Date) > 0 Then

If DateDiff ("D", objsfld. Datecreated,now ()) >= 0 Then

objFSO. DeleteFolder OBJSFLD

objFile.WriteLine "Backup file deleted:" & A

End If

Next

objFile.WriteLine "================================================================" & VBCRLF & VBCRLF

Objfile.close

Set objfile = Nothing

Set objFSO = Nothing

Set objfld = Nothing

Set Objsflds = Nothing

3, under the Deletebackupfolder.cmd command has the following content:

C:\Script\DeleteBackupFolder.vbs

4, first in the C:\TestFolder to create a few folders, the following figure:

5, first into the Sript directory run cmd script, then you will see the TestFolder directory just created the file has been deleted, and Log.txt file will display the following content:

================================================================

Database Documentation Report

Date: June 25, 2012

Time: 22:16:04

================================================================

Backup file deleted: New folder

Backup file deleted: New folder (2)

Backup file deleted: New folder (3)

Backup file deleted: New folder (4)

6, because it is to do the experiment, so I set the time in the VBS is greater than or equal to 0 (If DateDiff ("D", objsfld. Datecreated,now ()) >= 0), in the actual production environment, you can set the time you want to delete, such as delete one weeks ago, 0 to 7 is OK.

This article comes from "Robin's Home" blog, please be sure to keep this source http://winteragain.blog.51cto.com/1436066/909029

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.