VB - List all subfolders in a specific folder-VBForums
Results 1 to 12 of 12

Thread: VB - List all subfolders in a specific folder

  1. #1

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    VB - List all subfolders in a specific folder

    Add a reference to the "Microsoft Scripting Runtime":

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strStartPath As String
    3.     strStartPath = "C:\" 'ENTER YOUR START FOLDER HERE
    4.     ListFolder strStartPath
    5. End Sub
    6.  
    7. Private Sub ListFolder(sFolderPath As String)
    8.     Dim FS As New FileSystemObject
    9.     Dim FSfolder As Folder
    10.     Dim subfolder As Folder
    11.     Dim i As Integer
    12.    
    13.     Set FSfolder = FS.GetFolder(sFolderPath)
    14.  
    15.     For Each subfolder In FSfolder.SubFolders
    16.         DoEvents
    17.         i = i + 1
    18.         Debug.Print subfolder
    19.     Next subfolder
    20.     Set FSfolder = Nothing
    21.     MsgBox "Total sub folders in " & sFolderPath & " : " & i
    22. End Sub
    Last edited by manavo11; May 13th, 2003 at 04:41 PM.


    Has someone helped you? Then you can Rate their helpful post.

  2. #2
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752
    VB Code:
    1. Dim Mypath as string, MyName as String,iCount as integer
    2. iCount=0
    3. MyPath = "c:\"   ' Set the path.
    4. MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
    5. Do While MyName <> ""   ' Start the loop.
    6.    ' Ignore the current directory and the encompassing directory.
    7.    If MyName <> "." And MyName <> ".." Then
    8.       ' Use bitwise comparison to make sure MyName is a directory.
    9.       If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
    10.          Debug.Print MyName   ' Display entry only if it
    11.          iCount=iCount+1
    12.       End If   ' it represents a directory.
    13.    End If
    14.    MyName = Dir   ' Get next entry.
    15. Loop
    16.  
    17.  
    18. debug.print "No.of Folders in the selected path : " & iCount
    Last edited by faisalkm; Sep 15th, 2003 at 10:15 AM.
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  3. #3

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    Yep. Another way. Also works with files.

    Also, could you maybe edit your post and add the vbcode tags?

    Thanks


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752
    Never tried....How do I do it?
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  5. #5

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    Edit your post above and put at the start of the code [vbcode] and at the end of it put [/vbcode]


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Lively Member
    Join Date
    Oct 2003
    Posts
    127
    manavo11, how would you code it to find out if the directory exists or not, other then using an error handler? I tried using some of
    faisalkm's code, but it didnt work.

  7. #7

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    To check if a dir exists use this :

    VB Code:
    1. Private Sub Form_Load()
    2.     MsgBox DirExists("C:\windows")
    3.     MsgBox DirExists("C:\testing")
    4. End Sub
    5.  
    6. Private Function DirExists(sDir As String) As Boolean
    7.     If Len(Dir$(sDir, vbDirectory)) Then
    8.         DirExists = True
    9.     End If
    10. End Function


    Has someone helped you? Then you can Rate their helpful post.

  8. #8
    Addicted Member
    Join Date
    Jun 2004
    Location
    USA
    Posts
    172
    A real way:

    VB Code:
    1. Dim FS as Object
    2. Set FS = CreateObject("Scripting.FileSystemObject")
    3. If FS.FileExists("C:\myfile.txt") Then
    4.      'It exists!
    5. End If

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,092
    Originally posted by xjake88x
    A real way:

    VB Code:
    1. Dim FS as Object
    2. Set FS = CreateObject("Scripting.FileSystemObject")
    3. If FS.FileExists("C:\myfile.txt") Then
    4.      'It exists!
    5. End If
    Ack...using FSO and its late bound? Bad medicine.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  10. #10

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    Originally posted by crptcblade
    Ack...using FSO and its late bound? Bad medicine.
    Plus it should be like this :

    VB Code:
    1. If FS.FolderExists("C:\some dir") Then

    since the question regarded folder and not file existing...


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    New Member
    Join Date
    Aug 2005
    Posts
    1

    Re: VB - List all subfolders in a specific folder

    VB Code:
    1. Dim fs As Object
    2. Set fs = CreateObject("Scripting.FileSystemObject")
    3. Set fo = fs.GetFolder("c:\temp\)
    4. For Each x In fo.SubFolders
    5.  Debug.Print x.Name
    6. Next
    Last edited by svOi; Aug 2nd, 2005 at 03:58 PM.

  12. #12
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: VB - List all subfolders in a specific folder

    Another way
    vb Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    2. (ByVal hwnd As Long, _
    3. ByVal wMsg As Long, _
    4. ByVal wParam As Long, _
    5. lParam As Any) As Long
    6.  
    7. Private Const LB_DIR = &H18D
    8.  
    9. Private Const DDL_DIRECTORY = &H10
    10. Private Const DDL_ARCHIVE = &H20
    11. Private Const DDL_EXCLUSIVE = &H8000
    12.  
    13. Private Sub Command1_Click()
    14.     List1.Clear    
    15.     SendMessage List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal "C:\Windows\*.*"
    16. End Sub
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.