采用递归算法删除带有多级子目录的目录
option explicit
private sub command1_click()
dim strpathname as string
strpathname = “”
strpathname = inputbox(“请输入需要删除的文件夹名称∶”, “删除文件夹”)
if strpathname = “” then exit sub
on error goto errorhandle
setattr strpathname, vbnormal 此行主要是为了检查文件夹名称的有效性
recursetree strpathname
label1.caption = “文件夹” & strpathname & “已经删除!”
exit sub
errorhandle:
msgbox “无效的文件夹名称:” & strpathname
end sub
sub recursetree(currpath as string)
dim sfilename as string
dim newpath as string
dim spath as string
static oldpath as string
spath = currpath & “\”
sfilename = dir(spath, 31) 31的含义∶31=vbnormal+vbreadonly+vbhidden+vbsystem+vbvolume+vbdirectory
do while sfilename <> “”
if sfilename <> “.” and sfilename <> “..” then
if getattr(spath & sfilename) and vbdirectory then 如果是目录和文件夹
newpath = spath & sfilename
recursetree newpath
sfilename = dir(spath, 31)
else
setattr spath & sfilename, vbnormal
kill (spath & sfilename)
label1.caption = spath & sfilename 显示删除过程
sfilename = dir
end if
else
sfilename = dir
end if
doevents
loop
setattr currpath, vbnormal
rmdir currpath
label1.caption = currpath
end sub