Results 1 to 6 of 6

Thread: VB6 - Enhance file listing, and cope with sub-dir's [resolved]

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Resolved VB6 - Enhance file listing, and cope with sub-dir's [resolved]

    Hey everyone, well, i made this simple program to get all the files in a selected folder, and ten to search the 2nd level off folders in it, the porblem is most people will easily have more than two levels of folder to search within, how can i turn my example code into something that will deal with unlimited sub-dirs ?

    My code :

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strStartPath As String
    3.     strStartPath = InputBox("Please enter start path", "Search Path", "C:\My Music")
    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.     Set FSfolder = FS.GetFolder(sFolderpath)
    12.  
    13.     For Each subfolder In FSfolder.SubFolders
    14.         DoEvents
    15.         Debug.Print subfolder
    16.         List1.AddItem subfolder
    17.     Next subfolder
    18.     Set FSfolder = Nothing
    19. LoopList
    20. End Sub
    21.  
    22. Private Function ListSubFolder(sFolderpath As String)
    23.     Dim FS As New FileSystemObject
    24.     Dim FSfolder As Folder
    25.     Dim subfolder As Folder
    26.     Set FSfolder = FS.GetFolder(sFolderpath)
    27.  
    28.     For Each subfolder In FSfolder.SubFolders
    29.         DoEvents
    30.         Debug.Print subfolder
    31.         List2.AddItem subfolder
    32.     Next subfolder
    33.     Set FSfolder = Nothing
    34. End Function
    35.  
    36. Private Function LoopList()
    37. List1.ListIndex = 0
    38. Do Until List1.ListIndex = List1.ListCount - 1
    39.     ListSubFolder List1.Text
    40.     List1.ListIndex = List1.ListIndex + 1
    41.     DoEvents
    42. Loop
    43. GetFiles
    44. End Function
    45.  
    46. Private Function GetFiles()
    47. List1.ListIndex = 0
    48. List2.ListIndex = 0
    49. Do Until List1.ListIndex = List1.ListCount - 1
    50.     ListFiles List1.Text
    51.     List1.ListIndex = List1.ListIndex + 1
    52. DoEvents
    53. Loop
    54. Do Until List2.ListIndex = List2.ListCount - 1
    55.     ListFiles List2.Text
    56.     List2.ListIndex = List2.ListIndex + 1
    57. DoEvents
    58. Loop
    59. End Function
    60.  
    61. Private Sub ListFiles(strPath As String, Optional Extention As String)
    62.     Dim File As String
    63.         If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    64.         If Trim$(Extention) = "" Then
    65.         Extention = "*.*"
    66.     ElseIf Left$(Extention, 2) <> "*." Then
    67.         Extention = "*." & Extention
    68.     End If
    69.         File = Dir$(strPath & Extention)
    70.     Do While Len(File)
    71.         List3.AddItem strPath & File
    72.         File = Dir$
    73.     Loop
    74. End Sub
    75.  
    76. Private Sub List3_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    77. CD.Filter = "Winamp Playlist(*.M3U)|*.M3U"
    78. CD.ShowSave
    79. Open CD.FileName For Output As #1
    80. For i = 0 To List3.ListCount - 1
    81. Print #1, List3.List(i)
    82. Next
    83. Close #1
    84. End Sub

    I will attach my project also.
    Attached Files Attached Files
    Last edited by thegreatone; Jun 14th, 2005 at 11:18 AM.
    Zeegnahtuer?

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: VB6 - Enhance file listing, and cope with sub-dir's

    Well, there is a much easier way if you are interested

    Have a look at this

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: VB6 - Enhance file listing, and cope with sub-dir's

    Quote Originally Posted by sciguyryan
    Well, there is a much easier way if you are interested

    Have a look at this

    Cheers,

    RyanJ
    Slight problem with that though, its for searching for a file in the Dir's and Sub dir's, i simply want to list the files...

    Thanks for your input though.
    Last edited by thegreatone; Jun 13th, 2005 at 02:03 PM.
    Zeegnahtuer?

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: VB6 - Enhance file listing, and cope with sub-dir's

    Quote Originally Posted by thegreatone
    Slight problem with that though, its for searching for a file in the Dir's and Sub dir's, i simply want to list the files...

    Thanks for your input though.
    Oh, sorry...

    How about one of these?

    http://www.planet-source-code.com/vb...11311&lngWId=1
    http://www.freevbcode.com/ShowCode.A...isting&ID=1331


    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: VB6 - Enhance file listing, and cope with sub-dir's

    Hey Thanks, i tried repping you for your endeavours but i simply get

    Quote Originally Posted by vBulleten Message
    You must spread some Reputation around before giving it to sciguyryan again.
    EDIT: I thought i'd attach my code, and probably my project files for File Grabber.
    EDIT2 : Mayeb this could be posted in the Codebank for future reference ?

    VB Code:
    1. Function FolderContents(ByRef thePath As String, ByRef bolRecurseDirectories As Boolean, ByRef scratchfile As String)
    2.  
    3.     Dim objFSO
    4.     Set objFSO = CreateObject("Scripting.FileSystemObject")
    5.     Dim objFolder
    6.     Set objFolder = objFSO.getFolder(thePath)
    7.    
    8.     Call DisplayFolderContents(objFolder, bolRecurseDirectories, outFile)
    9.    
    10.  
    11.    
    12.  
    13.     Set objFolder = Nothing
    14.     Set objFSO = Nothing
    15. End Function
    16.  
    17.  
    18. Function DisplayFolderContents(objFolder, ByVal bolRecurseDirectories As Boolean, outFile)
    19.  
    20.     Dim objFile, strPath, strExtension
    21.  
    22.  
    23.     For Each objFile In objFolder.Files
    24.         strPath = objFile.Path
    25.         List1.AddItem (objFile.Path)
    26.     Next
    27.  
    28.     ' Recurse subdirectories if necessary
    29.     Dim objSubFolder
    30.  
    31.  
    32.     If bolRecurseDirectories Then
    33.  
    34.         For Each objSubFolder In objFolder.SubFolders
    35.             DisplayFolderContents objSubFolder, bolRecurseDirectories, outFile
    36.         Next
    37.  
    38.     End If
    39. LblCount.Caption = List1.ListCount
    40. End Function
    41.  
    42. Private Sub Command1_Click()
    43. Dim x As String
    44. Dim y As Boolean
    45. If Option1.Value = True Then y = True
    46. If Option2.Value = True Then y = False
    47. x = InputBox("Please enter start path", "Search Path", "C:\My Music")
    48. FolderContents x, y, ""
    49. End Sub
    50.  
    51. Private Sub Command2_Click()
    52. List1.Clear
    53. End Sub
    54.  
    55. Private Sub Command3_Click()
    56. On Error GoTo saveerr
    57. f = FreeFile
    58. CD.Filter = "Text Files (*.txt)|*.txt"
    59. CD.ShowSave
    60. Open CD.FileName For Output As #f
    61. For i = 0 To List1.ListCount - 1
    62. Print #f, List1.List(i)
    63. Next
    64. Close #f
    65. Exit Sub
    66. saveerr:
    67. If Err.Number <> 75 Then MsgBox Err.Number & ":" & Err.Description, vbCritical, "An Error has occurred"
    68. End Sub
    69.  
    70. Private Sub Command4_Click()
    71. On Error GoTo saveerr
    72. f = FreeFile
    73. Dim x As String
    74. x = InputBox("Which file extension do you wish to save as ?" & vbCrLf & "File extensions should look like this *.ext when entered below" & vbCrLf & "For example a playlist would be *.m3u", "Input File Extension", "*.txt")
    75. CD.Filter = x & "|" & x
    76. CD.ShowSave
    77. Open CD.FileName For Output As #f
    78. For i = 0 To List1.ListCount - 1
    79. Print #f, List1.List(i)
    80. Next
    81. Close #f
    82. Exit Sub
    83. saveerr:
    84. If Err.Number <> 75 Then MsgBox Err.Number & ":" & Err.Description, vbCritical, "An Error has occurred"
    85. End Sub
    Attached Files Attached Files
    Last edited by thegreatone; Jun 14th, 2005 at 11:33 AM.
    Zeegnahtuer?

  6. #6
    New Member
    Join Date
    Jun 2007
    Posts
    6

    Re: VB6 - Enhance file listing, and cope with sub-dir's

    Quote Originally Posted by thegreatone View Post
    Hey Thanks, i tried repping you for your endeavours but i simply get



    EDIT: I thought i'd attach my code, and probably my project files for File Grabber.
    EDIT2 : Mayeb this could be posted in the Codebank for future reference ?

    VB Code:
    1. Function FolderContents(ByRef thePath As String, ByRef bolRecurseDirectories As Boolean, ByRef scratchfile As String)
    2.  
    3.     Dim objFSO
    4.     Set objFSO = CreateObject("Scripting.FileSystemObject")
    5.     Dim objFolder
    6.     Set objFolder = objFSO.getFolder(thePath)
    7.    
    8.     Call DisplayFolderContents(objFolder, bolRecurseDirectories, outFile)
    9.    
    10.  
    11.    
    12.  
    13.     Set objFolder = Nothing
    14.     Set objFSO = Nothing
    15. End Function
    16.  
    17.  
    18. Function DisplayFolderContents(objFolder, ByVal bolRecurseDirectories As Boolean, outFile)
    19.  
    20.     Dim objFile, strPath, strExtension
    21.  
    22.  
    23.     For Each objFile In objFolder.Files
    24.         strPath = objFile.Path
    25.         List1.AddItem (objFile.Path)
    26.     Next
    27.  
    28.     ' Recurse subdirectories if necessary
    29.     Dim objSubFolder
    30.  
    31.  
    32.     If bolRecurseDirectories Then
    33.  
    34.         For Each objSubFolder In objFolder.SubFolders
    35.             DisplayFolderContents objSubFolder, bolRecurseDirectories, outFile
    36.         Next
    37.  
    38.     End If
    39. LblCount.Caption = List1.ListCount
    40. End Function
    41.  
    42. Private Sub Command1_Click()
    43. Dim x As String
    44. Dim y As Boolean
    45. If Option1.Value = True Then y = True
    46. If Option2.Value = True Then y = False
    47. x = InputBox("Please enter start path", "Search Path", "C:\My Music")
    48. FolderContents x, y, ""
    49. End Sub
    50.  
    51. Private Sub Command2_Click()
    52. List1.Clear
    53. End Sub
    54.  
    55. Private Sub Command3_Click()
    56. On Error GoTo saveerr
    57. f = FreeFile
    58. CD.Filter = "Text Files (*.txt)|*.txt"
    59. CD.ShowSave
    60. Open CD.FileName For Output As #f
    61. For i = 0 To List1.ListCount - 1
    62. Print #f, List1.List(i)
    63. Next
    64. Close #f
    65. Exit Sub
    66. saveerr:
    67. If Err.Number <> 75 Then MsgBox Err.Number & ":" & Err.Description, vbCritical, "An Error has occurred"
    68. End Sub
    69.  
    70. Private Sub Command4_Click()
    71. On Error GoTo saveerr
    72. f = FreeFile
    73. Dim x As String
    74. x = InputBox("Which file extension do you wish to save as ?" & vbCrLf & "File extensions should look like this *.ext when entered below" & vbCrLf & "For example a playlist would be *.m3u", "Input File Extension", "*.txt")
    75. CD.Filter = x & "|" & x
    76. CD.ShowSave
    77. Open CD.FileName For Output As #f
    78. For i = 0 To List1.ListCount - 1
    79. Print #f, List1.List(i)
    80. Next
    81. Close #f
    82. Exit Sub
    83. saveerr:
    84. If Err.Number <> 75 Then MsgBox Err.Number & ":" & Err.Description, vbCritical, "An Error has occurred"
    85. End Sub
    great code, however,how could you make it handle a greater map
    e.g. "c:\"

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