|
本帖最后由 ning84 于 2022-10-8 04:15 编辑
学习高手的编程思路,重新做一遍题目,获取所有目录名和文件名。
Dim Dict As Dictionary
关键语句
Set Dict = New Dictionary
Dict.Add ("F:\日出日落\"), ""
oKey = Dict.Keys
MyName = Dir(oKey(ii), vbDirectory)
If (GetAttr(oKey(ii) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
Dict.Add (oKey(ii) & MyName & "\"), "" '就往字典中添加这个次级目录名作为一个条目
End If
End If
- Dim Dict As Dictionary, oDic, oKey
- Dim MyName
- Dim ii
- Set Dict = New Dictionary
- Dict.Add ("F:\日出日落"), ""
- ii = 0
- Do While ii < Dict.Count
- oKey = Dict.Keys
- MyName = Dir(oKey(ii), vbDirectory)
-
- Do While MyName <> ""
- If MyName <> "." And MyName <> ".." Then
- Debug.Print oKey(ii) & MyName, GetAttr(oKey(ii) & MyName)
- If (GetAttr(oKey(ii) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
- Dict.Add (oKey(ii) & MyName & ""), "" '就往字典中添加这个次级目录名作为一个条目
- End If
- End If
- MyName = Dir '继续遍历寻找
- 'Debug.Print MyName ', Dir
- Loop
- ii = ii + 1
- Loop
复制代码
- Sub ll()
- 'Dim FullDict As Dictionary
- Dim FileDict As Dictionary
- Dim PathDict As Dictionary
- Dim oDic, oKey
- Dim MyName
- Dim ii
- Set PathDict = New Dictionary
- Set FileDict = New Dictionary
- PathDict.Add ("F:\日出日落\高德地图"), ""
-
- ii = 0
- Do While ii < PathDict.Count
- oKey = PathDict.Keys
- MyName = Dir(oKey(ii), vbDirectory)
-
- Do While MyName <> ""
- If MyName <> "." And MyName <> ".." Then
- 'Debug.Print oKey(ii) & MyName, GetAttr(oKey(ii) & MyName)
- If (GetAttr(oKey(ii) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
- PathDict.Add (oKey(ii) & MyName & ""), "" '就往字典中添加这个次级目录名作为一个条目
- End If
- End If
- MyName = Dir '继续遍历寻找
- 'Debug.Print MyName ', Dir
- Loop
- ii = ii + 1
- Loop
-
- FileDict.Add ("JPG"), ""
- For ii = 0 To PathDict.Count - 1
- oKey = PathDict.Keys(ii)
- MyFileName = Dir(oKey & "*.JPG")
- Do While MyFileName <> ""
- FileDict.Add (oKey & MyFileName), ""
- MyFileName = Dir
- Loop
- Next ii
-
- For ii = 0 To FileDict.Count - 1
- Debug.Print ii, FileDict.Keys(ii)
- Next ii
- For ii = 1 To PathDict.Count - 1
- Debug.Print PathDict.Keys(ii)
- Next ii
- End Sub
复制代码
|
|