I have following code which iterates thru folders (not finished code). All is OK, but when I use loop (IsLoop=True), then instead of listing files and folders of "C:\" it scans "C:\Users\Sektor\My Documents" folder. If I use single line call of EnumerateFiles procedure, then listing is correct. Where's error?
Code:
Option Explicit
#Const IsLoop = False
Private fso As FileSystemObject
Private textFile As TextStream
Sub GetFiles()
Dim drv As Drive
Dim lRow As Long
lRow = 0
Set fso = New FileSystemObject
Set textFile = fso.CreateTextFile(FileName:="files.txt", Overwrite:=True, Unicode:=False)
#If IsLoop Then
With wksFiles
.Cells.ClearContents
For Each drv In fso.Drives
If drv.IsReady Then
lRow = lRow + 1
.Cells(lRow, 1) = "Drive " & drv.Path & "\"
lRow = lRow + 1
EnumerateFiles drv.Path, lRow, 2
End If
Next
End With
#Else
wksFiles.Cells.ClearContents
EnumerateFiles "C:\", 2, 2
#End If
textFile.Close
Set fso = Nothing
Set drv = Nothing
Set textFile = Nothing
End Sub
Private Sub EnumerateFiles(ByVal theFolder As String, ByRef lRow As Long, ByVal lColumn As Long)
Dim objFile As File
Dim objFolder As Folder
Dim objRootFolder As Folder
Set objRootFolder = fso.GetFolder(theFolder)
With wksFiles
For Each objFolder In objRootFolder.SubFolders
.Cells(lRow, lColumn) = objFolder.Name
lRow = lRow + 1
Next
For Each objFile In objRootFolder.Files
.Cells(lRow, lColumn) = objFile.Name
lRow = lRow + 1
Next
End With
End Sub