使用vbs脚本定时删除N天前的文件
脚本放在定时任务里代替服务定时执行一些操作比较方便,下面是实现删除文件夹下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
脚本内容:
OptionExplicit dimmFSO '定义文件系统对象 dimwFSO '定义文件系统对象 DIMfilecount'文件计数 dimfoldercount'文件夹计数
dimfstream '定义读文件流对象 dimwstream '定义写文件流对象
dimfolder '文件夹对象 dimrootfolder '文件夹对象 dimfile '文件对象 dimSubdirs '文件夹集合对象 dimsubdir '文件夹对象 dimLoopFolder '文件夹对象 dimdopath '路径字符串
DimdelFlag delFlag=0 '0:不删除子目录 ,1:删除子目录
DIMWSH SETWSH=WSCRIPT.CreateObject("WSCRIPT.SHELL")'击活WScript.Shell对象 main()
submain() dimfilename filecount=0 foldercount=0 SetmFSO=CreateObject("Scripting.FileSystemObject") SetwFSO=CreateObject("Scripting.FileSystemObject") Setfstream=mFSO.OpenTextFile("DelFolderList.txt",1) filename=Cstr(Date) filename=filename+"_"+Replace(Cstr(Time),":","_") Setwstream=wFSO.CreateTextFile("log\"&filename&".log",TRUE) '循环读取一行filelist.txt中的内容 DoWhileTrue<>fstream.AtEndOfStream dopath=fstream.ReadLine Ifdopath="ONLY-DEL-FILES"Then delFlag=0 IfTrue<>fstream.AtEndOfStreamThen dopath=fstream.ReadLine EndIf EndIf Ifdopath="DEL-FOLDER-FILES"Then delFlag=1 IfTrue<>fstream.AtEndOfStreamThen dopath=fstream.ReadLine Endif EndIf ifmFSO.FolderExists(dopath)Then If1=delFlagThen'删除子目录
setrootfolder=mFSO.GetFolder(dopath) wstream.WriteLine(rootfolder.path&"下删除了以下文件:") del_file_subFolderrootfolder wstream.WriteLine(rootfolder.path&"下删除了以下文件夹:") del_folderFuncrootfolder 'del_folderFuncrootfolder wstream.WriteBlankLines(1) Else'只删除文件 setrootfolder=mFSO.GetFolder(dopath) wstream.WriteLine(rootfolder.path&"下删除了以下文件:") del_filerootfolder wstream.WriteBlankLines(1) EndIf else WSH.POPUP("文件夹“"&dopath&"”不存在或路径错误") wstream.WriteLine("文件夹“"&dopath&"”不存在或路径错误") endif Loop fstream.Close
'WSH.POPUP("共删除文件"&filecount&"个、文件夹"&foldercount&"个") endsub
subdel_file(folder) dimdf '标记 ForEachfileInfolder.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 EndIf Next endSub
''删除文件同时删除subfolder subdel_file_subFolder(folder) dimdf '标记 ForEachfileInfolder.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 EndIf Next '递归调用del_file函数,实现子目录文件的遍历删除 If(0<Folder.SubFolders.Count)then ForEachloopfolderinfolder.SubFolders del_file_subFolderloopfolder Next EndIf endsub
subdel_folderFunc(folder) dimloopsub dimtmp if0=folder.subfolders.CountThen '判断其下是否还有子文件夹,若无 if0=folder.files.Countthen '如果其下还有文件则退出(符合条件的文件上一步已删掉了) ifUcase(folder.path)<>Ucase(dopath)then '判断是否为根目录,是则退出,不是则删除 wstream.WriteLine(folder.path&vbTab&folder.DateCreated) folder.delete foldercount=foldercount+1 ''del_folderFunc(mFSO.GetFolder(delFolderPath)) '删除该文件夹后从根目录重新检查,以检查其父目录是否该删除 endif Endif else ForEachsubdirInfolder.subfolders '还有子文件夹则轮循每一个子文件夹 del_folderFunc(subdir) Next Endif
endsub