CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question For Each SubFolder in Folder [VB6]

    Given a Folder Tree (sample below) that contains files [Data.txt] I need to read into my code.
    C:\DATA\001\A\Data.txt
    C:\DATA\001\B\Data.txt
    C:\DATA\002\A\Data.txt
    C:\DATA\003\B\Data.txt
    ... etc ...

    I need to find a way to loop through each folder in C:\DATA\

    Pseudo-Code:
    For each numberFolder in C:\DATA
    For each letterFolder in numberFolder
    **DO SOMETHING**

    If this was C# I could use foreach, but I noticed VB6.0 also has a For Each X in Y and was hoping I could use but todate all attempts have failed (it seems to only like collections of some sort).
    Any ideas? Clues? Help? Thanks,

  2. #2
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: For Each SubFolder in Folder [VB6]

    You can do this with the FileSystemObject.

    Code:
    'Add reference to Microsoft Scripting Runtime
    Dim oFileSys As Scripting.FileSystemObject
    Dim oFolder As Scripting.Folder
    Dim oSubFolder As Scripting.Folder
    
    Set oFileSys = New Scripting.FileSystemObject
    Set oFolder = oFileSys.GetFolder("C:\DATA\")
    
    For Each oSubFolder In oFolder.SubFolders
        'Do something
    Next oSubFolder
    
    Set oFolder = Nothing
    Set oFileSys = Nothing
    Last edited by Comintern; March 28th, 2005 at 12:09 PM. Reason: Typos

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: For Each SubFolder in Folder [VB6]

    Quote Originally Posted by Comintern
    You can do this with the FileSystemObject.

    Code:
    'Add reference to Microsoft Scripting Runtime
    Dim oFileSys As Scripting.FileSystemObject
    Dim oFolder As Scripting.Folder
    Dim oSubFolder As Scripting.Folder
    
    Set oFileSys = New Scripting.FileSystemObject
    Set oFolder = oFileSys.GetFolder("C:\DATA\")
    
    For Each oSubFolder In oFolder.SubFolders
        'Do something
    Next oSubFolder
    
    Set oFolder = Nothing
    Set oFileSys = Nothing
    Very interesting code. But I'm sorry I cannot find anything about the filesystem object on my VB6.0 ?? Also nothing in help about that. Do I have to add any Dll or ocx before ?

    Jonny Poet
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: For Each SubFolder in Folder [VB6]

    Quote Originally Posted by JonnyPoet
    Very interesting code. But I'm sorry I cannot find anything about the filesystem object on my VB6.0 ?? Also nothing in help about that. Do I have to add any Dll or ocx before ?
    It's part of the Microsoft Scripting Runtime. You should be able to add a reference to it in VB6. The .dll is scrrun.dll. If for some reason you don't have it on your machine, you can download it here:

    http://msdn.microsoft.com/library/de...ist/webdev.asp

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: For Each SubFolder in Folder [VB6]

    Quote Originally Posted by Shaitan00
    Given a Folder Tree (sample below) that contains files [Data.txt] I need to read into my code.
    C:\DATA\001\A\Data.txt
    C:\DATA\001\B\Data.txt
    C:\DATA\002\A\Data.txt
    C:\DATA\003\B\Data.txt
    ... etc ...

    I need to find a way to loop through each folder in C:\DATA\

    Pseudo-Code:
    For each numberFolder in C:\DATA
    For each letterFolder in numberFolder
    **DO SOMETHING**

    If this was C# I could use foreach, but I noticed VB6.0 also has a For Each X in Y and was hoping I could use but todate all attempts have failed (it seems to only like collections of some sort).
    Any ideas? Clues? Help? Thanks,
    You have already done this question before and I have put you a zip in yor request.

    I put it here so it should not be any problem for you to pick it up. A newer Version which takes every letter from A to Z also if there are some missing is attached too. Its for exactly how you wrote. If you need other filenames and other subnames you can easily change the loop a bit. It loops trough it it doesn't use a for each.

    Jonny Poet
    Last edited by JonnyPoet; March 4th, 2009 at 07:43 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured