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