'模 块 名:GetFileList '功 能:返回指定文件夹的所有文件名列表 '返 回 值:成功/失败:True/False '参 数:GetFileList(指定文件夹路径, 文件数组,返回的文件类型) '引 用:无 '外部函数:无 '内部变量:[fName=文件名][i=用于循环] '调用方法: '++++++++++++++++++++++++++++++++++++ ' Dim FileName() As String, i As Long ' GetFileList "c:\", FileName ' For i = 0 To UBound(FileName) ' Debug.Print FileName(i) ' Next i '++++++++++++++++++++++++++++++++++++ Function GetFileList(ByVal Path As String, ByRef Filename() As String, Optional fExp As String = "*.*") As Boolean Dim fName As String, i As Long If right$(Path, 1) <> "\" Then Path = Path & "\" fName = Dir$(Path & fExp) i = 0 Do While fName <> "" ReDim Preserve Filename(i) As String Filename(i) = fName fName = Dir$ i = i + 1 Loop If i <> 0 Then ReDim Preserve Filename(i - 1) As String GetFileList = True Else GetFileList = False End If End Function
'添加Microsoft Scripting RunTime的引用 Private Sub Command1_Click() Dim fso As New FileSystemObject Dim fod As Folder Dim fil As File
Set fod = fso.GetFolder("c:\aaa") For Each fil In fod.Files fil.Delete True Next
Set fil = Nothing Set fod = Nothing Set fso = Nothing End Sub
删文件夹有点复杂,要考虑文件属性,是否可以删除, s = Dir(path & "\*", vbHidden Or vbArchive Or vbNormal Or vbReadOnly Or vbSystem) Do While s <> "" SetAttr tpath & "\" & s, vbNormal s = Dir() Loop kill path & "\*" RmDir path以上办法,还不能删除包括子文件夹的文件夹,你需先逐级删子文件夹,有点烦,自己想吧。 注意:Dir不能用于递归。
'功 能:返回指定文件夹的所有文件名列表
'返 回 值:成功/失败:True/False
'参 数:GetFileList(指定文件夹路径, 文件数组,返回的文件类型)
'引 用:无
'外部函数:无
'内部变量:[fName=文件名][i=用于循环]
'调用方法:
'++++++++++++++++++++++++++++++++++++
' Dim FileName() As String, i As Long
' GetFileList "c:\", FileName
' For i = 0 To UBound(FileName)
' Debug.Print FileName(i)
' Next i
'++++++++++++++++++++++++++++++++++++
Function GetFileList(ByVal Path As String, ByRef Filename() As String, Optional fExp As String = "*.*") As Boolean
Dim fName As String, i As Long
If right$(Path, 1) <> "\" Then Path = Path & "\"
fName = Dir$(Path & fExp)
i = 0
Do While fName <> ""
ReDim Preserve Filename(i) As String
Filename(i) = fName
fName = Dir$
i = i + 1
Loop
If i <> 0 Then
ReDim Preserve Filename(i - 1) As String
GetFileList = True
Else
GetFileList = False
End If
End Function
Private Sub Command1_Click()
Dim fso As New FileSystemObject
Dim fod As Folder
Dim fil As File
Set fod = fso.GetFolder("c:\aaa")
For Each fil In fod.Files
fil.Delete True
Next
Set fil = Nothing
Set fod = Nothing
Set fso = Nothing
End Sub
s = Dir(path & "\*", vbHidden Or vbArchive Or vbNormal Or vbReadOnly Or vbSystem)
Do While s <> ""
SetAttr tpath & "\" & s, vbNormal
s = Dir()
Loop
kill path & "\*"
RmDir path以上办法,还不能删除包括子文件夹的文件夹,你需先逐级删子文件夹,有点烦,自己想吧。
注意:Dir不能用于递归。