VBS递归删除文件及文件夹(默认是删除VC的临时文件的配置)
'可配置项
isDeleteFile=True
filterToDelete=".obj;.ilk;.tlb;.tli;.tlh;.tmp;.rsp;.pgc;.pgd;.ncb"
isDeleteSpetialFolder=False
filterToDeleteFolder="debug;release"
'======================================================================================
'dim ws,fso,f,fd,files,tmpname,url,result,preFix
set ws=createobject("wscript.shell")
set fso = CreateObject("Scripting.FileSystemObject")
dealpath=ws.currentdirectory
'dealpath="M:\BACKUP\递归删除VC中的临时文件"
set fd = fso.getfolder(dealpath)
set files=fd.files
'生成前缀
result=dealpath
result =replace(result ,"\","+")
result =replace(result ,".","_")
result =replace(result ,":","=")
'建立一个文本,存储文件的名字
result= dealpath & "\" & result & "_" & "fileinfos.txt"
set resultFile=fso.createtextfile(result,2,ture)
'开始工作
s=RecursiveDelete(dealpath,resultFile)
MsgBox "操作完成!文件信息保存于:" & result
'=============================================================================
'--------------------------------------------
'遍历一个路径下的所有文件及文件夹
'参数:folderspec 表示文件路径 例如:d:\javascript
'---------------------------------------------
Function RecursiveDelete(folderspec,logFile)
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(folderspec) Then '判断文件夹是否存在
s="文件夹不存在:" & folderspec
else
Set f = fso.GetFolder(folderspec) '返回与指定的路径中某文件夹相应的 Folder对象
Set fc = f.files
For Each f1 in fc
If isDeleteFile and IsNeedDlete(f1.name,filterToDelete) Then
curFullPathname=folderspec & "\" & f1.name & chr(34)
logFile.writeline "正在删除:" & curFullPathname
fso.deleteFile curFullPathname,true '删除文件
'outFile.WriteLine "DELETED:" & FormatPath(thePath) & "\" & myFile.Name
logFile.writeline "DELETED:" & curFullPathname
FileTotal = FileTotal + 1
End If
Next
Set ff=f.SubFolders
For Each f2 in ff
If isDeleteSpetialFolder and IsNeedDlete( f2.Path,filterToDeleteFolder) Then
curFolderStr = chr(34) & f2.Path & chr(34)
logFile.writeline "正在删除文件夹:" & curFolderStr
fso.DeleteFolder f2.Path
logFile.writeline "DELETED FOLDER:" & curFolderStr
Else
s=s&RecursiveDelete(f2.Path,logFile) '递归所有子文件夹里的文件
End If
Next
End If
RecursiveDelete = s
End Function
Function IsNeedDlete(toSearch,myFilter)
IsNeedDlete=False
myArray=Split(myFilter, ";", -1, 1)
For Each filterItem in myArray
If InStr(LCase(toSearch),LCase( filterItem )) and len(filterItem)>0 Then
IsNeedDlete=True
Exit For
End If
Next
End Function