腳本放在定時任務里代替服務定時執(zhí)行一些操作比較方便,下面是實現(xiàn)刪除文件夾下N天前創(chuàng)建的文件的vbs腳本,在配置文件 DelFolderList.txt 中配置要刪除的文件路徑,ONLY-DEL-FILES 下的路徑 是只刪除其下的文件,不刪除其內的子目錄的文件。DEL-FOLDER-FILES下的路徑 是刪除其內文件及其子目錄內的文件,子目錄為空時刪除目錄,刪除的文件list 放在log文件夾內。例配置文件DelFolder.txt 內容如下:
Option Explicit
dim mFSO '定義文件系統(tǒng)對象
dim wFSO '定義文件系統(tǒng)對象
DIM filecount '文件計數(shù)
dim foldercount '文件夾計數(shù)
dim fstream '定義讀文件流對象
dim wstream '定義寫文件流對象
dim folder '文件夾對象
dim rootfolder '文件夾對象
dim file '文件對象
dim Subdirs '文件夾集合對象
dim subdir '文件夾對象
dim LoopFolder '文件夾對象
dim dopath '路徑字符串
Dim delFlag
delFlag =0 ' 0: 不刪除子目錄 ,1:刪除子目錄
DIM WSH
SET WSH=WSCRIPT.CreateObject("WSCRIPT.SHELL")'擊活WScript.Shell對象
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)
'循環(huán)讀取一行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 ' 刪除子目錄
set rootfolder=mFSO.GetFolder(dopath)
wstream.WriteLine(rootfolder.path " 下刪除了以下文件:")
del_file_subFolder rootfolder
wstream.WriteLine(rootfolder.path " 下刪除了以下文件夾:")
del_folderFunc rootfolder
'del_folderFunc rootfolder
wstream.WriteBlankLines(1)
Else '只刪除文件
set rootfolder=mFSO.GetFolder(dopath)
wstream.WriteLine(rootfolder.path " 下刪除了以下文件:")
del_file rootfolder
wstream.WriteBlankLines(1)
End If
else
WSH.POPUP("文件夾“" dopath "”不存在或路徑錯誤")
wstream.WriteLine("文件夾“" dopath "”不存在或路徑錯誤")
end if
Loop
fstream.Close
'WSH.POPUP("共刪除文件" filecount "個、文件夾" foldercount "個")
end sub
sub del_file(folder)
dim df '標記
For Each file In folder.files
df=DateDiff("d",file.DateCreated,Now)
If (df>1) Then '1天前的文件
wstream.WriteLine(folder.path "\" file.Name vbTab file.DateCreated)
file.Delete()
filecount=filecount+1
End If
Next
end Sub
''刪除文件 同時刪除subfolder
sub del_file_subFolder(folder)
dim df '標記
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
'遞歸調用del_file函數(shù),實現(xiàn)子目錄文件的遍歷刪除
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 '判斷其下是否還有子文件夾,若無
if 0=folder.files.Count then '如果其下還有文件則退出(符合條件的文件上一步已刪掉了)
if Ucase(folder.path)>Ucase(dopath) then '判斷是否為根目錄,是則退出,不是則刪除
wstream.WriteLine(folder.path vbTab folder.DateCreated)
folder.delete
foldercount=foldercount+1
'' del_folderFunc(mFSO.GetFolder(delFolderPath)) '刪除該文件夾后從根目錄重新檢查,以檢查其父目錄是否該刪除
end if
End if
else
For Each subdir In folder.subfolders '還有子文件夾則輪循每一個子文件夾
del_folderFunc(subdir)
Next
End if
end sub