脚本放在定时任务里代替服务定时执行一些操作比较方便,下面是实现删除文件夹下N天前创建的文件的vbs脚本,在配置文件 DelFolderList.txt 中配置要删除的文件路径,ONLY-DEL-FILES 下的路径 是只删除其下的文件,不删除其内的子目录的文件。DEL-FOLDER-FILES下的路径 是删除其内文件及其子目录内的文件,子目录为空时删除目录,删除的文件list 放在log文件夹内。例配置文件DelFolder.txt 内容如下:
ONLY-DEL-FILES
E:\Code\test
DEL-FOLDER-FILES
E:\Code\subfolder
脚本内容:
Option Explicit
dim mFSO '定义文件系统对象
dim wFSO '定义文件系统对象
DIM filecount '文件计数
dim foldercount '文件夹计数
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)
'循环读取一行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函数,实现子目录文件的遍历删除
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