获取某目录下的所有子目录路径及名称和文件的路径及名称Public Sub SeachFile(ByVal strPath As String) Dim Fso As Object Dim Fol As Object Dim Fil As Object Set Fso = CreateObject("Scripting.FileSystemObject") Set Fol = Fso.GetFolder(strPath)
For Each Fil In Fol.Files Debug.Print Fil.Path '打印路径及文件名 Next For Each Fol In Fol.subfolders '遍历子文件夹 Debug.Print Fol.Path '打印子目录的路径及名称 SeachFile Fol Next End SubPrivate Sub Command1_Click() SeachFile ("C:\Test\") End Sub
到 www.mndsoft.com 搜索 “文件查找,文件搜索”
'<CSCM> '************************************************************************* ' 功能描述:'列举文件夹下的所有文件名,加到数组里 传入变体型返回数组 '************************************************************************* '</CSCM> Public Function ListFolderFiles(ByRef myArrayFiles, strFolder As String) Dim strFileName As String Dim intI As Integer strFileName = Dir( strFolder & "/") If strFileName <> "" Then ReDim myArrayFiles(0) As String intI = 0 While strFileName <> "" Debug.Print strFileName ReDim Preserve myArrayFiles(intI) As String myArrayFiles(intI) = strFileName intI = intI + 1 strFileName = Dir() Wend End If End Function只获得目录下的文件名, 要是获得目录下的文件夹里面的文件 得递归 递归后在调用上面的函数 http://community.csdn.net/Expert/topic/4681/4681460.xml?temp=.9482233
Private Sub Command1_Click() Dim fd As String Set fso = CreateObject("Scripting.FileSystemObject") fd = "C:\x" Call getFilenm(fd) MsgBox fn End Sub
Function getFilenm(fdnm As String) Dim obFd, fl, sfd Set obFd = fso.GetFolder(fdnm) For Each fl In obFd.Files fn = fn & fl.Name & Chr(10) Next If obFd.SubFolders.Count > 0 Then For Each sfd In obFd.SubFolders Call getFilenm(sfd.Path) Next End If End Function
Dim Fso As Object
Dim Fol As Object
Dim Fil As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fol = Fso.GetFolder(strPath)
For Each Fil In Fol.Files
Debug.Print Fil.Path '打印路径及文件名
Next
For Each Fol In Fol.subfolders '遍历子文件夹
Debug.Print Fol.Path '打印子目录的路径及名称
SeachFile Fol
Next
End SubPrivate Sub Command1_Click()
SeachFile ("C:\Test\")
End Sub
'*************************************************************************
' 功能描述:'列举文件夹下的所有文件名,加到数组里 传入变体型返回数组
'*************************************************************************
'</CSCM>
Public Function ListFolderFiles(ByRef myArrayFiles, strFolder As String)
Dim strFileName As String
Dim intI As Integer
strFileName = Dir( strFolder & "/")
If strFileName <> "" Then
ReDim myArrayFiles(0) As String
intI = 0
While strFileName <> ""
Debug.Print strFileName
ReDim Preserve myArrayFiles(intI) As String
myArrayFiles(intI) = strFileName
intI = intI + 1
strFileName = Dir()
Wend
End If
End Function只获得目录下的文件名, 要是获得目录下的文件夹里面的文件 得递归 递归后在调用上面的函数
http://community.csdn.net/Expert/topic/4681/4681460.xml?temp=.9482233
EMAIL: [email protected]
or [email protected]
Dim fn As String
Private Sub Command1_Click()
Dim fd As String
Set fso = CreateObject("Scripting.FileSystemObject")
fd = "C:\x"
Call getFilenm(fd)
MsgBox fn
End Sub
Function getFilenm(fdnm As String)
Dim obFd, fl, sfd
Set obFd = fso.GetFolder(fdnm)
For Each fl In obFd.Files
fn = fn & fl.Name & Chr(10)
Next
If obFd.SubFolders.Count > 0 Then
For Each sfd In obFd.SubFolders
Call getFilenm(sfd.Path)
Next
End If
End Function
的方法我用了,很好用,但是如果文件夹下面有子文件,程序就会出错。谢谢大家的帮助