VBScript删除子文件夹和文件的程序,自己定义删多深

'VBScript删除子文件夹和文件的程序,可以自己定义删多深。采用的recursively删除,免除打开子文件后报错。
'http://www.theswamp.org/index.php?topic=26167.0

Function deletefilesandfolders (foldername, deep)
	dim fso,fldr
	set fso = createobject("scripting.filesystemobject")
	set fldr = fso.getfolder(foldername)
	
	MsgBox "The folder " & fldr & " exists so now to delete all folders inside.", vbInformation

	intAnswer = Msgbox("Do you want to delete these files", vbYesNo, "Delete Files")
	If intAnswer = vbNo Then
		Exit Function
	End If
	
	deep = UBound(Split (foldername, "\")) + deep
	recurse fldr, deep
	
	Set fso = Nothing 
	Set fldr = Nothing
End Function

Sub recurse(byref fldr, deep)
	dim subfolders,files,folder,file
	set subfolders = fldr.subfolders
	set files = fldr.files
	for each file in files
		on error resume next
		name = file.name
		file.Delete True
	   If Err Then
		 MsgBox "Error deleting:" & Name & " - " & Err.Description, vbInformation
	   Else
		 MsgBox "Deleted:" & Name, vbInformation
	   End If
	   On Error GoTo 0
	next  
	for each folder in subfolders
		recurse folder, deep
		If UBound(Split (folder.path, "\")) > deep Then
		    name = folder.name
			folder.Delete True
			If Err Then
				MsgBox "Error deleting:" & Name & " - " & Err.Description, vbInformation
			Else
				MsgBox "Deleted:" & Name, vbInformation
			End If
			On Error GoTo 0
		End If
	next   
	set subfolders = nothing
	set files = nothing
End Sub

deletefilesandfolders "C:\NewTemp", 0
'0==delete all subfolders . 1==leave subfolders 1 deep . 2==leave subfolders 2 deep...and so on...


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值