bassnsjp
07-06-2013, 10:58 AM
I found a macro online to get files from a particular directory and all its subdirectories. Getting the files from the parent directory is not a problem. Attempting to get the filenames from the next level directory is where I get the object required error message. Actually it is this line:
Set objFolder = objFSO.GetFolder(SubFolderName)
I've hardcoded SubFolderName as well as allowing the system to acquire the pathname (SubFolder.path) and I get the same results. What is odd is that the same line is used in the main routine and works with no problem. The debug.print just before the Set command displays the correct pathname. Any assistance would be greatly appreciated thank you in advance.
Here is the code:
Sub Getfilesnsubf()
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\bassmaster\Documents\TestFindnReplaceData"
' objStartFolder = InputBox("Enter pathname: ", "Path to search")
If (Right$(objStartFolder, 1)) <> "\" Then
objStartFolder = objStartFolder & "\"
End If
Set objFolder = objFSO.GetFolder(objStartFolder)
Debug.Print objFolder.path
Set colFiles = objFolder.Files
For Each objFile In colFiles
Debug.Print "Filename: " & objFile.Name
Next
ShowSubFolders objFSO.GetFolder(objStartFolder)
End Sub
Sub ShowSubFolders(Folder)
Debug.Print " # of subfolders: " & Folder.subfolders.Count
For Each SubFolder In Folder.subfolders
If (Right$(SubFolder.path, 1)) <> "\" Then
SubFolderName = SubFolder.path & "\"
End If
Debug.Print " SubFolder name: " & SubFolderName
Set objFolder = objFSO.GetFolder(SubFolderName)
Set colFiles = objFolder.Files
For Each objFile In colFiles
Debug.Print " Filename in subfolder: " & objFile.Name
Next
ShowSubFolders SubFolder
Next
End Sub
Set objFolder = objFSO.GetFolder(SubFolderName)
I've hardcoded SubFolderName as well as allowing the system to acquire the pathname (SubFolder.path) and I get the same results. What is odd is that the same line is used in the main routine and works with no problem. The debug.print just before the Set command displays the correct pathname. Any assistance would be greatly appreciated thank you in advance.
Here is the code:
Sub Getfilesnsubf()
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\bassmaster\Documents\TestFindnReplaceData"
' objStartFolder = InputBox("Enter pathname: ", "Path to search")
If (Right$(objStartFolder, 1)) <> "\" Then
objStartFolder = objStartFolder & "\"
End If
Set objFolder = objFSO.GetFolder(objStartFolder)
Debug.Print objFolder.path
Set colFiles = objFolder.Files
For Each objFile In colFiles
Debug.Print "Filename: " & objFile.Name
Next
ShowSubFolders objFSO.GetFolder(objStartFolder)
End Sub
Sub ShowSubFolders(Folder)
Debug.Print " # of subfolders: " & Folder.subfolders.Count
For Each SubFolder In Folder.subfolders
If (Right$(SubFolder.path, 1)) <> "\" Then
SubFolderName = SubFolder.path & "\"
End If
Debug.Print " SubFolder name: " & SubFolderName
Set objFolder = objFSO.GetFolder(SubFolderName)
Set colFiles = objFolder.Files
For Each objFile In colFiles
Debug.Print " Filename in subfolder: " & objFile.Name
Next
ShowSubFolders SubFolder
Next
End Sub