目录
4 step4 遍历文件夹的(含子文件)内的所有文件夹和文件
Function file_dir1()
fp = "C:\Users\Administrator\Desktop\ppp\*.*"
fn = Dir(fp)
Debug.Print fn '避免会少第1个文件
Do While fn <> ""
fn = Dir
Debug.Print fn
Loop
End Function
'----------------step2---------------------------------------------
Sub Folder2()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'确定根目录
ff = "C:\Users\Administrator\Desktop\ppp"
'遍历根目录下的子文件夹
Dim folder As Object ' 当前正在遍历的文件夹
Set folder = fso.GetFolder(ff)
Call SubProcessFolder2(folder)
End Sub
Sub SubProcessFolder2(folder)
'子文件夹
Dim subFolder As Object
For Each subFolder In folder.subfolders
Debug.Print subFolder.Path
' 如果还有更深层次的子文件夹,则进行递归调用
If subFolder.subfolders.Count > 0 Then
Call SubProcessFolder2(subFolder)
End If
Next subFolder
End Sub
'-------------step3---------------------------------------------
Sub Folder3()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'确定根目录
ff = "C:\Users\Administrator\Desktop\ppp"
'遍历根目录下的文件
Call file3(ff)
'遍历根目录下的子文件夹
Dim folder As Object ' 当前正在遍历的文件夹
Set folder = fso.GetFolder(ff)
Call SubProcessFolder3(folder)
End Sub
Sub file3(folder)
fp = folder & "\*.*"
fn = Dir(fp)
Do While fn <> ""
Debug.Print folder & "\" & fn
fn = Dir() '这句话应该放在打印后面
Loop
End Sub
Sub SubProcessFolder3(folder)
'子文件夹
Dim subFolder As Object
For Each subFolder In folder.subfolders
Call file3(subFolder)
' 如果还有更深层次的子文件夹,则进行递归调用
If subFolder.subfolders.Count > 0 Then
Call SubProcessFolder3(subFolder)
End If
Next subFolder
End Sub
'--------step4---------------------------------------------
Sub Folder4()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'确定根目录
ff = "C:\Users\Administrator\Desktop\ppp"
Debug.Print ff
'遍历根目录下的文件
Call file4(ff)
'遍历根目录下的子文件夹
Dim folder As Object ' 当前正在遍历的文件夹
Set folder = fso.GetFolder(ff)
Call SubProcessFolder4(folder)
End Sub
Sub file4(folder)
fp = folder & "\*.*"
fn = Dir(fp)
Do While fn <> ""
Debug.Print fn 'folder & "\" & fn
fn = Dir() '这句话应该放在打印后面
Loop
End Sub
Sub SubProcessFolder4(folder)
'子文件夹
Dim subFolder As Object
For Each subFolder In folder.subfolders
Debug.Print subFolder.Path
Call file4(subFolder)
' 如果还有更深层次的子文件夹,则进行递归调用
If subFolder.subfolders.Count > 0 Then
Call SubProcessFolder4(subFolder)
End If
Next subFolder
End Sub