本文主要介紹如何利用VBS在WINDOWS上刪除過期檔案,雖然不是很常用,但久久就會有人問一次,寫下來當作備忘。

DeleteFolder.vbs

' ====== Code Start ===================
Option Explicit
 
WScript.Echo("作業開始執行:" & Date & " " & Time)
 
Dim FSO, agoDays, modifiedDate, delFolder
 
' 設定過期檔案預期天數
agoDays = 3
' 欲刪除檔案所在之目錄
delFolder = "D:\Temp"
 
Set FSO = CreateObject("Scripting.FileSystemObject")
 
modifiedDate = DateAdd("d", -agoDays, Date)
 
DelFilesInFolder FSO.GetFolder(delFolder)
 
Sub DelFilesInFolder(folder)
    Dim file, subFolder

    For Each file In folder.Files
        If ((file.DateLastModified <= modifiedDate)) Then
            file.delete
        End If
    Next

    ' 如果遇到子目錄,也要進去檢查並刪除
    For Each subFolder in folder.SubFolders
        DelFilesInFolder subFolder
    Next
End Sub
 
WScript.Echo("作業執行完畢:" & Date & " " & Time) 
' ====== Code End ===================

,

mark528 發表在 痞客邦 PIXNET 留言(0) 人氣()