Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 29th, 2010, 03:59 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default Weird behavior of FileSystemObject [Excel 2010]

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
 
Old July 29th, 2010, 04:21 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default

I found what causes error: Drive.Path must be concatenated with "\". All is OK!





Similar Threads
Thread Thread Starter Forum Replies Last Post
WorksheetFunction.Match function problem [Excel 2010] sektor Excel VBA 1 March 10th, 2010 06:22 PM
Visual Studio 2010 in Focus at Great Indian Developer Summit 2010 shaguf5575 ASP.NET 1.x and 2.0 Application Design 0 February 24th, 2010 07:03 AM
Visual Studio 2010 in Focus at Great Indian Developer Summit 2010 shaguf5575 ASP.NET 1.x and 2.0 Application Design 0 February 24th, 2010 07:02 AM
Weird post back behavior mjhoagland Classic ASP Professional 3 December 26th, 2006 10:31 AM
weird c# or browser behavior Ed Tijgen General .NET 0 August 18th, 2004 12:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.