Use the VBS script to periodically delete files that are N days ago _vbs

Source: Internet
Author: User
Tags parent directory readline

The script is placed in a timed task instead of a service to perform some operations more convenient, the following is the implementation of the Delete folder under the N-day file created by the VBS script, in the configuration file DelFolderList.txt to remove the file path, only-del-files under the path is to delete only the files under it, without deleting the subdirectories in the file. The path under Del-folder-files is to delete the files within the file and its subdirectories, delete the directory when the subdirectory is empty, and delete the file list in the log folder. The example configuration file DelFolder.txt the following contents:

Only-del-files
E:\Code\test
Del-folder-files
E:\Code\subfolder

Script content:

Copy Code code as follows:

Option Explicit
Dim Mfso ' defines file system objects
Dim Wfso ' defines file system objects
DIM filecount ' file count
Dim Foldercount ' Folder Count

Dim FStream ' defines read file stream objects
Dim Wstream ' defines write file stream objects

Dim Folder ' Folder Object
Dim RootFolder ' Folder Object
Dim file ' File Object
Dim Subdirs ' Folder Collection Object
Dim SubDir ' Folder Object
Dim Loopfolder ' Folder Object
Dim Dopath ' Path String

Dim Delflag
Delflag = 0 ' 0: Do not delete subdirectories, 1: Delete subdirectories

DIM WSH
SET Wsh=wscript. CreateObject ("WSCRIPT.") SHELL ")" Wscript.Shell object
Main ()

Sub Main ()
Dim filename
FileCount = 0
Foldercount = 0
Set Mfso = CreateObject ("Scripting.FileSystemObject")
Set Wfso = CreateObject ("Scripting.FileSystemObject")
Set fstream = Mfso.opentextfile ("DelFolderList.txt", 1)
Filename=cstr (Date)
Filename=filename + "_" + Replace (Cstr (Time), ":", "_")
Set Wstream = Wfso.createtextfile ("log\" & filename & ". Log", TRUE)
' Iterate through the contents of a row of filelist.txt
Do While True <> fstream. AtEndOfStream
Dopath = FStream. ReadLine
If Dopath = "Only-del-files" Then
Delflag =0
If True <> fstream. AtEndOfStream Then
Dopath = FStream. ReadLine
End If
End If

If Dopath = "Del-folder-files" Then
Delflag =1
If True <> fstream. AtEndOfStream Then
Dopath = FStream. ReadLine
End If
End If

If Mfso.folderexists (Dopath) Then
If 1 = delflag Then ' deletes subdirectories

Set Rootfolder=mfso.getfolder (Dopath)
Wstream. WriteLine (Rootfolder.path &) The following files were deleted: ")
Del_file_subfolder RootFolder
Wstream. WriteLine (Rootfolder.path &) The following folder was deleted: ")
Del_folderfunc RootFolder
' Del_folderfunc RootFolder
Wstream. WriteBlankLines (1)
Else ' Delete only files
Set Rootfolder=mfso.getfolder (Dopath)
Wstream. WriteLine (Rootfolder.path &) The following files were deleted: ")
Del_file RootFolder
Wstream. WriteBlankLines (1)
End If
Else
WSH. POPUP (Folder & Dopath & "No presence or path error")
Wstream. WriteLine ("folder" & Dopath & "No presence or path error")
End If
Loop
FStream. Close

' WSH. POPUP ("Delete Files" & FileCount & "Folders" & Foldercount & "")
End Sub

Sub Del_file (folder)
Dim df ' Tag
For each file in Folder.files
Df=datediff ("D", file. Datecreated,now)
If (df>1) Then ' 1 days ago File
Wstream. WriteLine (Folder.path & "\" & file. Name & vbTab & file. DateCreated)
File. Delete ()
Filecount=filecount+1
End If
Next
End Sub

' Delete file and delete subfolder
Sub Del_file_subfolder (folder)
Dim df ' Tag
For each file in Folder.files
Df=datediff ("D", file. Datecreated,now)
If (df>1) Then
Wstream. WriteLine (Folder.path & "\" & file. Name & vbTab & file. DateCreated)
File. Delete ()
Filecount=filecount+1
End If
Next
' Recursive call del_file function to implement the traversal deletion of subdirectory files
If (0 < Folder.SubFolders.Count) Then
For each loopfolder in folder. Subfolders
Del_file_subfolder Loopfolder
Next
End If
End Sub


Sub Del_folderfunc (folder)
Dim loopsub
Dim tmp
If 0=folder.subfolders.count Then ' Determine if there are subfolders under it, if no
If 0=folder.files.count then ' if there are files under it then exit (the qualifying file has been deleted in the previous step)
If Ucase (Folder.path) <>ucase (Dopath) Then ' determines whether it is the root directory, is exited, is not deleted
Wstream. WriteLine (Folder.path & vbTab & folder. DateCreated)
Folder.Delete
Foldercount=foldercount+1
' Del_folderfunc (Mfso.getfolder (Delfolderpath)) ' Deletes the folder and then checks it from the root to see if its parent directory should be deleted
End If
End If
Else
For every subdir in Folder.subfolders ' and subfolders then round each subfolder
Del_folderfunc (SubDir)
Next
End If

End Sub

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.