Directory/File Lister-VBForums
Results 1 to 3 of 3

Thread: Directory/File Lister

  1. #1

    Thread Starter
    Web developer Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    12,443

    Directory/File Lister

    This is a basic directory and file lister I created about 10 years back. I though I would paste the code here incase others could improve on it.

    vb Code:
    1. 'You need:
    2. '1 Drive List Box called Drive1
    3. '1 Directory List Box called Dir1
    4. '1 File lisrt box called File1
    5. '1 Combo box called cmbType
    6. '2 Command Buttons
    7. Dim n As Integer
    8.  
    9. Private Sub CmbType_Click()
    10. On Error GoTo err:
    11. File1.Pattern = CmbType.Text
    12. Exit Sub
    13. err:
    14. MsgBox ("Please select a file type!")
    15. End Sub
    16.  
    17. Private Sub Command1_Click()
    18. 'Close the program
    19. Unload Me
    20. End Sub
    21. 'Directory and File Lister
    22. 'Version 1.0
    23. 'Author: Nightwalker83
    24. 'Website: http://aaronspehr.net/
    25. Private Sub Command2_Click()
    26. 'Save list to a text file.
    27. For n = 1 To File1.ListCount
    28. Open App.Path & "\Files.txt" For Append As #1
    29. Print #1, File1.List(n)
    30. Close #1
    31. Next n
    32. End Sub
    33.  
    34. Private Sub Dir1_Change()
    35. File1.Path = Dir1.Path
    36. End Sub
    37.  
    38. Private Sub Drive1_Change()
    39. On Error GoTo err:
    40. Dir1.Path = Drive1.Drive
    41. Exit Sub
    42. err:
    43. MsgBox ("Please choose another drive!")
    44. End Sub
    45.  
    46. Private Sub Form_Load()
    47. frmDFL.Caption = "Directory and File Lister"
    48. Command1.Caption = "Exit"
    49. Command2.Caption = "Save"
    50. CmbType.Text = "File Type:"
    51. 'Library Files.
    52. CmbType.AddItem "*.dll"
    53. 'Word Documents.
    54. CmbType.AddItem "*.doc"
    55. 'Executables.
    56. CmbType.AddItem "*.exe"
    57. 'Text Files.
    58. CmbType.AddItem "*.txt"
    59. 'All Files.
    60. CmbType.AddItem "*.*"
    61. End Sub

    Nightwalker
    Last edited by Nightwalker83; Mar 21st, 2012 at 10:43 PM. Reason: Fixing comments!
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,132

    Re: Directory/File Lister

    Reorganized a couple of lines:

    Code:
    'You need:
    '1 DriveListBox called Drive1
    '1 DirListBox called Dir1
    '1 FileListBox called File1
    '1 ComboBox called cmbType
    '2 CommandButtons
    
    Dim n As Integer
    
    Private Sub CmbType_Click()
        On Error GoTo err
        File1.Pattern = CmbType.Text
        Exit Sub
    err:
        MsgBox "Please select a file type!", vbExclamation
    End Sub
    
    Private Sub Command1_Click()
       'Close the program
        Unload Me
    End Sub
    
    'Directory and File Lister
    'Version 1.0
    'Author: Nightwalker83
    'Website: http://aaronspehr.net/
    
    Private Sub Command2_Click()
       'Save list to a text file.
        Open App.Path & "\Files.txt" For Append As #1    '<-- Moved out of the loop
            For n = 1 To File1.ListCount
                Print #1, File1.List(n)
            Next n
        Close #1
    End Sub
    
    Private Sub Dir1_Change()
        File1.Path = Dir1.Path
    End Sub
    
    Private Sub Drive1_Change()
        On Error GoTo err
        Dir1.Path = Drive1.Drive
        Exit Sub
    err:
        MsgBox "Please choose another drive!", vbExclamation
    End Sub
    
    Private Sub Form_Load()
        Me.Caption = "Directory and File Lister"    '<-- Made generic
        Command1.Caption = "Exit"
        Command2.Caption = "Save"
        CmbType.Text = "File Type:"
       'Library Files.
        CmbType.AddItem "*.dll"
       'Word Documents.
        CmbType.AddItem "*.doc"
       'Executables.
        CmbType.AddItem "*.exe"
       'Text Files.
        CmbType.AddItem "*.txt"
       'All Files.
        CmbType.AddItem "*.*"
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0

    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Web developer Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    12,443

    Re: Directory/File Lister

    Thanks for error checking my code! I haven't checked it for errors since I originally wrote it back in 2002.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

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.