VBS递归删除文件及文件夹(默认是删除VC的临时文件的配置)

 

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值