Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 29th, 2010, 03:59 AM
Authorized User
 
Join Date: Mar 2008
Location: , , Russia.
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
Reply With Quote
  #2 (permalink)  
Old July 29th, 2010, 04:21 AM
Authorized User
 
Join Date: Mar 2008
Location: , , Russia.
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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:59 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.