腳本放在定時(shí)任務(wù)里代替服務(wù)定時(shí)執(zhí)行一些操作比較方便,下面是實(shí)現(xiàn)刪除文件夾下N天前創(chuàng)建的文件的vbs腳本,在配置文件 DelFolderList.txt 中配置要?jiǎng)h除的文件路徑,ONLY-DEL-FILES 下的路徑 是只刪除其下的文件,不刪除其內(nèi)的子目錄的文件。DEL-FOLDER-FILES下的路徑 是刪除其內(nèi)文件及其子目錄內(nèi)的文件,子目錄為空時(shí)刪除目錄,刪除的文件list 放在log文件夾內(nèi)。例配置文件DelFolder.txt 內(nèi)容如下:
ONLY-DEL-FILES
E:\Code\test
DEL-FOLDER-FILES
E:\Code\subfolder
腳本內(nèi)容:
dim fstream '定義讀文件流對(duì)象
dim wstream '定義寫(xiě)文件流對(duì)象
dim folder '文件夾對(duì)象
dim rootfolder '文件夾對(duì)象
dim file '文件對(duì)象
dim Subdirs '文件夾集合對(duì)象
dim subdir '文件夾對(duì)象
dim LoopFolder '文件夾對(duì)象
dim dopath '路徑字符串
Dim delFlag
delFlag =0 ' 0: 不刪除子目錄 ,1:刪除子目錄
DIM WSH
SET WSH=WSCRIPT.CreateObject("WSCRIPT.SHELL")'擊活WScript.Shell對(duì)象
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中的內(nèi)容
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 "”不存在或路徑錯(cuò)誤")
wstream.WriteLine("文件夾“" dopath "”不存在或路徑錯(cuò)誤")
end if
Loop
fstream.Close
'WSH.POPUP("共刪除文件" filecount "個(gè)、文件夾" foldercount "個(gè)")
end sub
sub del_file(folder)
dim df '標(biāo)記
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
''刪除文件 同時(shí)刪除subfolder
sub del_file_subFolder(folder)
dim df '標(biāo)記
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
'遞歸調(diào)用del_file函數(shù),實(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 '判斷其下是否還有子文件夾,若無(wú)
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 '還有子文件夾則輪循每一個(gè)子文件夾
del_folderFunc(subdir)
Next
End if
end sub
標(biāo)簽:四平 銅川 湖州 西安 文山 邢臺(tái) 鶴壁
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用vbs腳本定時(shí)刪除N天前的文件》,本文關(guān)鍵詞 使用,vbs,腳本,定時(shí),刪除,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。