目录
0 要解决的问题:如何遍历windows文件夹查找文件夹 (目录)和文件
0.1 原始文件夹和文件情况
0.2 目标:
0.3 为了达到复杂的目标,下面将分步骤做,从简单到难,逐步解决更难的问题
1 step1:遍历文件夹(不包含子文件)内的文件数
1.1 语法注意点:
1.1.1 取得文件夹下的文件名的方法
1.1.2 dir(path) 查找文件夹 和查找文件的语法不同
1.1.3 判断文件夹内所有文件是否遍历完成的判断条件
1.1.4 注意第一个文件的特殊性
1.2 测试代码
2 step2: 遍历文件夹内的所有子文件夹的名字
2.1 创建根目录的主函数
2.1.1 fso对象
2.1.2 传递 根目录给 子目录函数
2.1.3 子目录函数里要做递归处理
2.2 测试代码
3 step3 遍历文件夹的(含子文件)内的所有文件
3.1 因为第1个目录先读到,所以其目录名和文件名都要在,子目录函数外,单独显示
3.2 显示文件函数
3.3 测试代码
4 step4 遍历文件夹的(含子文件)内的所有文件夹和文件
4.1 综合前面的功能
4.2 注意:根目录的目录名,文件遍历都要写在主函数里
4.3 测试代码
5 step5: 遍历全部子目录,查找特定 目标 文件!
5.1 假设要查找一个target文件,是否存在这个目录下和详细路径
5.2 文件搜索函数,增加对target文件的比对判断
5.3 测试代码
6 STEP6: 批量创建子目录 和 批量创建文件(先判断是否存在) 并写入内容
6.1 这部分的目标是
6.2 用 mkdir() 批量创建文件夹/目录
6.3 批量创建文件
6.3.1 先查文件是否已经存在
6.3.2 批量创建文件
6.3.3 写入内容
6.3.4 打开和关闭文件
6.4 测试代码
fn = Dir(fp)
Debug.Print fn '避免会少第1个文件
Do While fn <> ""
fn = Dir
Debug.Print fn
Loop
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
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'确定根目录
ff = "C:\Users\Administrator\Desktop\ppp"
'遍历根目录下的子文件夹
Dim folder As Object ' 当前正在遍历的文件夹
Set folder = fso.GetFolder(ff)
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
'----------------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
- ff = "C:\Users\Administrator\Desktop\ppp"
- debug.print ff
- Call file3(ff)
Sub file3(folder)
fp = folder & "\*.*"
fn = Dir(fp)
Do While fn <> ""
Debug.Print folder & "\" & fn
fn = Dir() '这句话应该放在打印后面
Loop
End Sub
fn = Dir(fp)
Do While fn <> ""
Debug.Print folder & "\" & fn
fn = Dir() '这句话应该放在打印后面
Loop
'-------------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
Sub file5(folder)
target = "20.txt"fp = folder & "\*.*"
fn = Dir(fp)
Do While fn <> ""
Debug.Print fn 'folder & "\" & fn
If fn = target Then
Debug.Print "已经找到,位于" & folder & "\" & fn
Else
Debug.Print "此目录下找不到名为" & target & "的文件"
End If
fn = Dir() '这句话应该放在打印后面
Loop
End Sub
'--------step5---------------------------------------------
Sub Folder5()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'确定根目录
'C:\Users\snow\Desktop
' ff = "C:\Users\Administrator\Desktop\ppp"
ff = "C:\Users\snow\Desktop\ppp"
Debug.Print ff
'遍历根目录下的文件
Call file5(ff)
'遍历根目录下的子文件夹
Dim folder As Object ' 当前正在遍历的文件夹
Set folder = fso.GetFolder(ff)
Call SubProcessFolder5(folder)
End Sub
Sub file5(folder)
target = "20.txt"
fp = folder & "\*.*"
fn = Dir(fp)
Do While fn <> ""
Debug.Print fn 'folder & "\" & fn
If fn = target Then
Debug.Print "已经找到,位于" & folder & "\" & fn
Else
Debug.Print "此目录下找不到名为" & target & "的文件"
End If
fn = Dir() '这句话应该放在打印后面
Loop
End Sub
Sub SubProcessFolder5(folder)
'子文件夹
Dim subFolder As Object
For Each subFolder In folder.subfolders
Debug.Print subFolder.Path
Call file5(subFolder)
' 如果还有更深层次的子文件夹,则进行递归调用
If subFolder.subfolders.Count > 0 Then
Call SubProcessFolder5(subFolder)
End If
Next subFolder
End Sub
C:\Users\snow\Desktop\ppp
p1.txt
此目录下找不到名为20.txt的文件
p2.txt
此目录下找不到名为20.txt的文件
p3.txt
此目录下找不到名为20.txt的文件
C:\Users\snow\Desktop\ppp\1
11.txt
此目录下找不到名为20.txt的文件
C:\Users\snow\Desktop\ppp\2
20.txt
已经找到,位于C:\Users\snow\Desktop\ppp\2\20.txt
'这段只能创建文件夹
For i = 1 To 10
If Dir(ff & "\B" & i & ".txt") = "" Then
MkDir (ff & "\B" & i)
End If
Next
'这段创建文件,且先查文件是否已经存在,写入内容
Count = 0
For i = 1 To 20
Filename = ff & "\AA" & i & ".txt"
'If FileLen(Filename) = 0 Then '判断文件长度这里不好使
If Dir(Filename) = "" Then
filenumber = FreeFile
Open Filename For Output As filenumber
Print #filenumber, i
Count = Count + 1
Close filenumber
End If
Next
Debug.Print "运行完成,count=" & Count
filenumber = FreeFile
Open Filename For Output As filenumber
Print #filenumber, i
Close filenumber
Open Filename For Output As filenumber
Print #filenumber, i
Close filenumber
Sub file_create1()
ff = "C:\Users\snow\Desktop\ppp"
fp = ff & "\*.*"
'这段只能创建文件夹
For i = 1 To 10
If Dir(ff & "\B" & i & ".txt") = "" Then
MkDir (ff & "\B" & i)
End If
Next
'这段创建文件,且先查文件是否已经存在,写入内容
Count = 0
For i = 1 To 20
Filename = ff & "\AA" & i & ".txt"
'If FileLen(Filename) = 0 Then '判断文件长度这里不好使
If Dir(Filename) = "" Then
filenumber = FreeFile
Open Filename For Output As filenumber
Print #filenumber, i
Count = Count + 1
Close filenumber
End If
Next
Debug.Print "运行完成,count=" & Count
End Sub