M
Martin Stender
Hi All - I looking for a way to parse through folder and files - that is,
from a specified basefolder and all the way down, recursively.
On msdn, I found this
(http://msdn.microsoft.com/library/d...re/html/deconreturningfilesfromfilesystem.asp)
that seemed like a step in the right direction, but I can't get the code to
actually work.
I have a userform, with a simple button, that triggers the main function
like this:
Private Sub CommandButton1_Click()
Dim dctDict As ScriptingDictionary
Set dctDict = New ScriptingDictionary
GetFiles "C:\temp",dctDict, True
End Sub
*** Here is the code from the article, which I have in a module
*********************************************
Function GetFiles(strPath As String, _
dctDict As ScriptingDictionary, _
Optional blnRecursive As Boolean) As Boolean
' This procedure returns all the files in a directory into
' a Dictionary object. If called recursively, it also returns
' all files in subfolders.
Dim fsoSysObj As FileSystemObject
Dim fdrFolder As Folder
Dim fdrSubFolder As Folder
Dim filFile As File
' Return new FileSystemObject.
Set fsoSysObj = New FileSystemObject
On Error Resume Next
' Get folder.
Set fdrFolder = fsoSysObj.GetFolder(strPath)
If Err <> 0 Then
' Incorrect path.
GetFiles = False
GoTo GetFiles_End
End If
On Error GoTo 0
' Loop through Files collection, adding to dictionary.
For Each filFile In fdrFolder.Files
dctDict.Add filFile.Path, filFile.Name
Next filFile
' If Recursive flag is true, call recursively.
If blnRecursive Then
For Each fdrSubFolder In fdrFolder.SubFolders
GetFiles fdrSubFolder.Path, dctDict, True
Next fdrSubFolder
End If
' Return True if no error occurred.
GetFiles = True
GetFiles_End:
Exit Function
End Function
*********************************************
Sub TestGetFiles()
' Call to test GetFiles function.
Dim dctDict As ScriptingDictionary
Dim varItem As Variant
' Create new dictionary.
Set dctDict = New Dictionary
' Call recursively, return files into Dictionary object.
If GetFiles(GetTempDir, dctDict, True) Then
' Print items in dictionary.
For Each varItem In dctDict
Debug.Print varItem
Next
End If
End Sub***********************************************
It keeps failing with an "User defined type not defined" ... but the code
is Microsofts own ... am I using it wrong
Any suggestions?
Best
Martin
from a specified basefolder and all the way down, recursively.
On msdn, I found this
(http://msdn.microsoft.com/library/d...re/html/deconreturningfilesfromfilesystem.asp)
that seemed like a step in the right direction, but I can't get the code to
actually work.
I have a userform, with a simple button, that triggers the main function
like this:
Private Sub CommandButton1_Click()
Dim dctDict As ScriptingDictionary
Set dctDict = New ScriptingDictionary
GetFiles "C:\temp",dctDict, True
End Sub
*** Here is the code from the article, which I have in a module
*********************************************
Function GetFiles(strPath As String, _
dctDict As ScriptingDictionary, _
Optional blnRecursive As Boolean) As Boolean
' This procedure returns all the files in a directory into
' a Dictionary object. If called recursively, it also returns
' all files in subfolders.
Dim fsoSysObj As FileSystemObject
Dim fdrFolder As Folder
Dim fdrSubFolder As Folder
Dim filFile As File
' Return new FileSystemObject.
Set fsoSysObj = New FileSystemObject
On Error Resume Next
' Get folder.
Set fdrFolder = fsoSysObj.GetFolder(strPath)
If Err <> 0 Then
' Incorrect path.
GetFiles = False
GoTo GetFiles_End
End If
On Error GoTo 0
' Loop through Files collection, adding to dictionary.
For Each filFile In fdrFolder.Files
dctDict.Add filFile.Path, filFile.Name
Next filFile
' If Recursive flag is true, call recursively.
If blnRecursive Then
For Each fdrSubFolder In fdrFolder.SubFolders
GetFiles fdrSubFolder.Path, dctDict, True
Next fdrSubFolder
End If
' Return True if no error occurred.
GetFiles = True
GetFiles_End:
Exit Function
End Function
*********************************************
Sub TestGetFiles()
' Call to test GetFiles function.
Dim dctDict As ScriptingDictionary
Dim varItem As Variant
' Create new dictionary.
Set dctDict = New Dictionary
' Call recursively, return files into Dictionary object.
If GetFiles(GetTempDir, dctDict, True) Then
' Print items in dictionary.
For Each varItem In dctDict
Debug.Print varItem
Next
End If
End Sub***********************************************
It keeps failing with an "User defined type not defined" ... but the code
is Microsofts own ... am I using it wrong
Any suggestions?
Best
Martin