Results 1 to 22 of 22

Thread: list all files in directory, add to list box

  1. #1

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943

    list all files in directory, add to list box

    well, i think the title says it all... no FSO plz... i know the dir function can do this, but i don't know how...
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  2. #2
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    Toronto, Ontario
    Posts
    280
    What does FSO mean?

    Why wouldn't you just use the File List Box control? (The intrinsic one)

  3. #3
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Good day Eric This should solve your problem.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim arrFiles() As String
    3.     Dim i As Long
    4.    
    5.     arrFiles = DirList("C:\windows\*.*")
    6.     'You need the \*.* or it won't work...
    7.    
    8.     For i = 0 To UBound(arrFiles)
    9.         List1.AddItem arrFiles(i)
    10.     Next
    11. End Sub
    12.  
    13. Function DirList(strDir As String) As String()
    14.     'returns a 0 based array with the files in strDir
    15.     'Can contain a pattern such as "*.*"
    16.     'NOTE - subfolders not supported at this time
    17.     Dim Count As Integer
    18.     Dim sFiles() As String
    19.     Dim sFile As String
    20.    
    21.     sFile = Dir$(strDir)
    22.     Count = -1
    23.     Do
    24.         Count = Count + 1
    25.         ReDim Preserve sFiles(Count)
    26.         sFiles(Count) = sFile
    27.         sFile = Dir$
    28.     Loop Until sFile = ""
    29.     DirList = sFiles
    30. End Function
    You just proved that sig advertisements work.

  4. #4
    Lively Member slx47's Avatar
    Join Date
    Apr 2002
    Location
    US
    Posts
    127
    im not sure how to do it but try going here:

    www.google.com

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long,  ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    2.  
    3. Private Sub Command1_Click()
    4.     Dim r As Long
    5.     r = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\*.*")
    6. End Sub
    -= a peet post =-

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    peet: That is a VERY nice little snippet of code!


  7. #7

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    so many choices of code, so much use for them... now i gotta decide which one i want to use...
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  8. #8

    Thread Starter
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    Originally posted by Kevin_0011
    What does FSO mean?

    Why wouldn't you just use the File List Box control? (The intrinsic one)
    fso = teh file system object = teh crap

    i don't use the file list box control because im retarded and i like having things done in code, i use the least amount of intrinsic controls i can just because it makes me feel special to do it myself (by copying other peoples codes )
    Government is another way to say better…than…you.
    It’s like ice but no pick, a murder charge that won’t stick,
    it’s like a whole other world where you can smell the food,
    but you can’t touch the silverware.
    Huh, what luck. Fascism you can vote for.
    Humph, isn’t that sweet?
    And we’re all gonna die some day, because that’s the American way
    -Stone Sour

  9. #9
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by peet
    VB Code:
    1. Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long,  ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    2.  
    3. Private Sub Command1_Click()
    4.     Dim r As Long
    5.     r = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\*.*")
    6. End Sub
    cooooooooooooooooolness...
    You just proved that sig advertisements work.

  10. #10
    Myrrdan
    Guest

    kewl code snippet

    Question... in
    <code>
    Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

    i have an error of Object variable not set on the list1.hwnd, what is list1 supposed to be?

    Reason I ask, I was thinking of making a small program just to search my cd's and make a list of every cd, and whats on it...
    This would come in handy..
    thanks

  11. #11
    Si_the_geek
    Guest

    Re: kewl code snippet

    Originally posted by Myrrdan
    i have an error of Object variable not set on the list1.hwnd, what is list1 supposed to be?
    ..erm, that's a list box, just add one to your form (it will be called list1 by default).

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

    Re: list all files in directory, add to list box

    Quote Originally Posted by nishantp View Post
    Good day Eric This should solve your problem.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim arrFiles() As String
    3.     Dim i As Long
    4.    
    5.     arrFiles = DirList("C:\windows\*.*")
    6.     'You need the \*.* or it won't work...
    7.    
    8.     For i = 0 To UBound(arrFiles)
    9.         List1.AddItem arrFiles(i)
    10.     Next
    11. End Sub
    12.  
    13. Function DirList(strDir As String) As String()
    14.     'returns a 0 based array with the files in strDir
    15.     'Can contain a pattern such as "*.*"
    16.     'NOTE - subfolders not supported at this time
    17.     Dim Count As Integer
    18.     Dim sFiles() As String
    19.     Dim sFile As String
    20.    
    21.     sFile = Dir$(strDir)
    22.     Count = -1
    23.     Do
    24.         Count = Count + 1
    25.         ReDim Preserve sFiles(Count)
    26.         sFiles(Count) = sFile
    27.         sFile = Dir$
    28.     Loop Until sFile = ""
    29.     DirList = sFiles
    30. End Function
    How could we be able to also let the other files show ,those in submaps ?

    I thought by doing it by putting all the other maps in a different list and to use the command every time, but is there a beter way ?

  13. #13
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: list all files in directory, add to list box

    Yet another alternative (perhaps more traditional):
    Code:
    Private Sub Command1_Click()
    Dim NameFile As String, SubDir As String
    SubDir = CurDir$ & "\*.*" ' Change CurDir to whatever directory is being searched
    NameFile = Dir$(SubDir)
    Do While NameFile <> vbNullString
        List1.AddItem NameFile
        NameFile = Dir$
    Loop
    End Sub
    Doctor Ed

  14. #14
    New Member
    Join Date
    May 2009
    Posts
    11

    Re: list all files in directory, add to list box

    You can use the FindFile and FindNextFile api's. I personally find it much better to use api's.

    I've got a class somewhere's, just give me a minute to find it, and I will post it.

  15. #15
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: list all files in directory, add to list box

    Quote Originally Posted by Jarhead View Post
    You can use the FindFile and FindNextFile api's. I personally find it much better to use api's.

    I've got a class somewhere's, just give me a minute to find it, and I will post it.
    Not sure how these API's are going to beat post #13. 'Course, I could be wrong.
    Doctor Ed

  16. #16
    New Member
    Join Date
    May 2009
    Posts
    11

    Re: list all files in directory, add to list box

    Basic example, and you will need to change it to fit your needs.
    Attached Files Attached Files

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

    Re: list all files in directory, add to list box

    Thanks guy's, although I found another code :

    VB Code:
    1. Function FolderContents(ByRef thePath As String, ByRef bolRecurseDirectories As Boolean, ByRef scratchfile As String)
    2.     Dim objFSO
    3.     Set objFSO = CreateObject("Scripting.FileSystemObject")
    4.     Dim objFolder
    5.     Set objFolder = objFSO.getFolder(thePath)
    6.     Call DisplayFolderContents(objFolder, bolRecurseDirectories, outFile)
    7.     Set objFolder = Nothing
    8.     Set objFSO = Nothing
    9. End Function
    10. Function DisplayFolderContents(objFolder, ByVal bolRecurseDirectories As Boolean, outFile)
    11.     Dim objFile, strPath, strExtension
    12.     For Each objFile In objFolder.Files
    13.         strPath = objFile.Path
    14.         If FileLen(Trim(strPath)) <> 0 Then List3.AddItem (objFile.Path)
    15.     Next
    16.     ' Recurse subdirectories if necessary
    17.     Dim objSubFolder
    18.     If bolRecurseDirectories Then
    19.         For Each objSubFolder In objFolder.SubFolders
    20.             DisplayFolderContents objSubFolder, bolRecurseDirectories, outFile
    21.         Next
    22.     End If
    23. End Function
    24. Private Sub Command1_Click()
    25. FolderContents Text1.Text, True, ""
    26. end sub

    This can handle little maps, but if I'd try to use it on c:
    then the program stops responding...

  18. #18
    New Member
    Join Date
    May 2009
    Posts
    11

    Re: list all files in directory, add to list box

    He said no fso, lol.

  19. #19
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: list all files in directory, add to list box

    Quote Originally Posted by Jarhead View Post
    He said no fso, lol.
    That's another reason why I like post #13.
    Doctor Ed

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

    Re: list all files in directory, add to list box

    Quote Originally Posted by Jarhead View Post
    He said no fso, lol.
    I don't mind if it's fso or not
    only if it's fast

  21. #21
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: list all files in directory, add to list box

    Did you try CodeDoc's suggestion in #13?

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

    Re: list all files in directory, add to list box

    Quote Originally Posted by Hack View Post
    Did you try CodeDoc's suggestion in #13?
    yes, but it only shows the files in c , not in the submaps
    and I want all the files to be showed.
    I know this could take a long time, but mine stoppes completely

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