使用vbs脚本定时删除N天前的文件
脚本放在定时任务里代替服务定时执行一些操作比较方便,下面是实现删除文件夹下N天前创建的文件的vbs脚本,在配置文件 DelFolderList.txt 中配置要删除的文件路径,ONLY-DEL-FILES 下的路径 是只删除其下的文件,不删除其内的子目录的文件。DEL-FOLDER-FILES下的路径 是删除其内文件及其子目录内的文件,子目录为空时删除目录,删除的文件list 放在log文件夹内。例配置文件DelFolder.txt 内容如下:
ONLY-DEL-FILES
E:\Code\testDEL-FOLDER-FILESE:\Code\subfolder脚本内容:
复制代码 代码如下:
Option Explicitdim 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 filenamefilecount = 0foldercount = 0Set 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&nbhttp://www.devze.comsp; If True <> fstream.AtEndOfStream Then dopath = fstream.ReadLine End If End If If dopath = "DEL-FOLD开发者_C教程ER-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 ifLoopfstream.Close&编程客栈nbsp;'WSH.POPUP("共删除文件" & filecount & "个、文件夹" & foldercount & "个")
end subsub 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 Nextend 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 fohttp://www.devze.comlder.SubFolders del_file_subFolder loopfolder Next End Ifend sub sub del_folderFunc(folder) dim loopsub dim tmp if 0=folder.subfolders.Count Then '判断其下是否还有子文件夹,若无 if 0=folder.files.Count then&nAwehGtTibsp; '如果其下还有文件则退出(符合条件的文件上一步已删掉了) if Ucase(folder.path)<>Ucase(dopath) then '判断是否为根目录,是则退出,不是则删除&编程客栈nbsp; 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 ifend sub
精彩评论