2011年12月21日 星期三

使用vbs定期刪除和搬移特定檔案

' 指定所有變數必須事先宣告才能使用  
Option Explicit  
 
WScript.Echo("作業開始執行:" & Date & " " & Time)  
 
' 宣告變數  
Dim FSO, agoDays, modifiedDate, delFolder  
 
' 請將下面的變數值換成你要的  
' == 開始 ==  
' 指定 n 天前的檔案,現在是 3 天前  
agoDays = 3  
' 欲刪除檔案所在之目錄  
delFolder = "D:\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  
            file.delete 
        End If  
    Next  
 
    ' 如果遇到子目錄,也要進去檢查並刪除  
    For Each subFolder in folder.SubFolders  
        DelFilesInFolder subFolder  
    Next  
End Sub  
 
WScript.Echo("作業執行完畢:" & Date & " " & Time) 

轉載自http://ithelp.ithome.com.tw/question/10009001

沒有留言:

張貼留言