' 指定所有變數必須事先宣告才能使用
Option Explicit
WScript.Echo("作業開始執行:" & Date & " " & Time)
' 宣告變數
Dim FSO, agoDays, modifiedDate, delFolder
' 請將下面的變數值換成你要的
' == 開始 ==
' 指定 n 天前的檔案,現在是 3 天前
agoDays = 3
' 欲刪除檔案所在之目錄
delFolder = "e:\Temp"
' == 結束 ==
' 建立檔案系統物件(File System Object)
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
' 判斷副檔名
If (UCase(FSO.GetExtensionName(file.Name)) = "TXT") Then
file.delete
End If
End If
Next
' 如果遇到子目錄,也要進去檢查並刪除
For Each subFolder in folder.SubFolders
DelFilesInFolder subFolder
Next
End Sub
WScript.Echo("作業執行完畢:" & Date & " " & Time)
2009年3月24日
VB Script 刪除檔案
訂閱:
文章 (Atom)