Results 1 to 37 of 37

Thread: list files in a folder?

  1. #1

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Resolved list files in a folder?

    is a way with standard vb?
    Last edited by nareth; Dec 19th, 2004 at 04:00 PM.

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: list files in a folder?

    sure, the Dir() function.
    Code:
    Dim F As String
    
    F = Dir$("C:\*.*", vbNormal Or vbArchive Or vbReadOnly Or vbSystem Or vbHidden)
    
    Do While LenB(F) > 0
      Debug.Print F
      F = Dir$
    Loop
    casey.

  3. #3

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    thanks =)


  4. #4

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    is there a way to count the files in the directory without this method?

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: list files in a folder?

    if you mean WITH this method, then here it is.
    Code:
    Dim F As String
    Cnt = 1
    F = Dir$("C:\*.*", vbNormal Or vbArchive Or vbReadOnly Or vbSystem Or vbHidden)
    
    Do While LenB(F) > 0
      Debug.Print F
      F = Dir$
      Cnt = Cnt + 1
    Loop
    Debug.Print Cnt
    What files do you want to count, if you don't want to use this method?
    You could just count them without displaying them.

  6. #6

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    with OUT

    because im using a dynamic array so it would take long to keep using redim preserve

  7. #7
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: list files in a folder?

    What is wrong about using redim preserve? Anyway... If you don't want to do that, then use this:

    Code:
     Dim strFile As String
     Dim colFiles As Collection
     Set colFiles = New Collection
     
     strFile = Dir$("C:\*.*", vbNormal Or vbArchive Or vbRedOnly Or vbSystem Or vbHidden)
     
     Do While LenB(strFile) > 0
       colFile.Add strFile
       strFile = Dir$
     Loop
     
     '.... Some other code here, you use the files.
     'The count of the number of files will be colFile.Count
     
     Set colFile = Nothing
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  8. #8

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    you dont get it.

    when in the loop and i use redim preserve evrytime inside the loop on each file it would be very slow.

  9. #9

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    API?

    anyone?

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: list files in a folder?

    nareth,

    Come on, think...


    Only redim preserve every one hundreth time or so.

  11. #11

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    im asking something

  12. #12
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: list files in a folder?

    nareth,

    Not meaning to be harsh. Just attempting to get you to see the easier way other than doing something everytime.

  13. #13

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    look there could be 1000 files in this folder or even one million. i just need the fastest way if you like it or not.

  14. #14

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    is there anyone whos willing to HELP instead of telling me what todo. i know what im doing

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: list files in a folder?

    because im using a dynamic array so it would take long to keep using redim preserve
    It was your idea.

    ?????

  16. #16

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    Quote Originally Posted by randem
    It was your idea.

    ?????
    please delete this post im getting pissed now

  17. #17
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: list files in a folder?

    dude you need to chill, he was only trying to help out. if your not going to accept other peoples suggestions when you ask a question then i think you need to reconsider posting in the first place
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  18. #18
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: list files in a folder?

    Quote Originally Posted by nareth
    please delete this post im getting pissed now
    You should really chill off, man. When you feel like doing something then look at the sample by Randy Birch that demonstrates how to use FindFirstFile(), FindNextFile() api functions.

  19. #19
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: list files in a folder?

    Quote Originally Posted by nareth
    is there a way to count the files in the directory without this method?
    Code:
    Private Sub Form_Load()
    Dim fso, f
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("c:\")
    Set fc = f.Files
    Debug.Print fc.Count
    
    End Sub
    is there anyone whos willing to HELP instead of telling me what todo. i know what im doing
    I got your back on this nareth. You ask a question and should get an answer, not question about why you need to do it or possibly better ways to do it. Unless you explain your project fully, none here can give you a better way because they can't see the big picture. But I think that most ppl here, are genuinely trying to help, and sometimes a little patience is cool.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  20. #20

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    thank you kebo, you are the only person along with vbasicgirl that really helped me out =)

    and its not that i got pissed because he tried to help me. i see him posting rubbish all time -.-
    Last edited by nareth; Dec 19th, 2004 at 04:03 PM.

  21. #21
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: list files in a folder?

    Just out of curiosity, nareth: did you bother looking at the sample project from that link I posted? The thing is that FSO is nothing more than Windows API wrapper and is noticeably slower that the source plus you have no control over results it's outputting. It's easy enough to use (no doubt about that) but why not learn how to use the source itself and have full control over entire process ???

    Same thing applies to CommonDialog, SysInfo and some other controls available in VB ...

    Best regards.

  22. #22

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    the api would be the same as:

    Dim F As String
    Cnt = 1
    F = Dir$("C:\*.*", vbNormal Or vbArchive Or vbReadOnly Or vbSystem Or vbHidden)

    Do While LenB(F) > 0
    Debug.Print F
    F = Dir$
    Cnt = Cnt + 1
    Loop
    Debug.Print Cnt

  23. #23
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: list files in a folder?

    Nareth, Tec-Nico gave you a perfect example using a collection of what you needed to use.
    There is NO NEED what so ever to start getting nasty with people who are trying to help you. If you are using a dynamic array, then you are doing it "wrong" and there is much better coding, like people suggested!!!

    You clearly do NOT know what you're doing if your using dynamic arrays. There are much better methods.

    Maybe they are essential to your app and maybe it's something that you cannot change. If this is the case then explain so. don't get nasty with users!

    People are ONLY trying to help you, can you not see that. Plus you really need to start giving more details in your responses.

    Woka

  24. #24

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    woka you dont get the situation

    i just asked how i could get the file count a DIFRENT way and poeple started to telling me what todo i only asked a simple question.

    im FORCED to do it with a dynamic array for speed issues.
    guess i would loop trough files first to count and then redim the dynamic array and laod the files
    Last edited by nareth; Dec 20th, 2004 at 06:49 AM.

  25. #25
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: list files in a folder?

    Well explain that then clearly.
    Why not use a collection though?
    You can load the files in a collection, and then do:
    Code:
    colFiles.Count
    Or you can do what Random suggested which would be:
    Code:
    Dim strFile As String
    Dim strFiles() As String
    Dim lngIndex As Long
    
       strFile = Dir$("C:\*.*", vbNormal Or vbArchive Or vbReadOnly Or vbSystem Or vbHidden)
       ReDim strFile(0) As String
       lngIndex = -1
       Do While LenB(strFile) > 0
         lngIndex = lngIndex + 1
         If lngIndex = UBound(strFiles) Then
            ReDim Preserve strFiles(lngIndex+500) As String
         End If
         strFiles(lngIndex)=strFile
         strFile = Dir$
       Loop
       ReDim Preserve strFiles(lngIndex) As String
    This loads them dynamically. But it is quick as it redim preserves 500 at a time, and not for EVERY file.

    This is what you should use, but you had a go at people suggesting it.

    Woka

  26. #26

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    it would make memory overhead as im using a pretty big module class

    isnt it faster to first count hte files with the loop and then redim and then loop again?

  27. #27
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: list files in a folder?

    Quote Originally Posted by nareth
    it would make memory overhead as im using a pretty big module class

    isnt it faster to first count hte files with the loop and then redim and then loop again?
    No, it wouldn't. Try it. I don't think anyways.
    What memory overhead???
    A collection will use slightly more memory that an array, but it has many other benefits.

    Woka

  28. #28
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: list files in a folder?

    Also, using API functions to get the filenames, instead of Dir(), is WAY faster. Dunno what they are though...do a search on this forum for "find and fiels and API"

    WOOF

  29. #29
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: list files in a folder?

    Quote Originally Posted by nareth
    the api would be the same as:

    Dim F As String
    Cnt = 1
    F = Dir$("C:\*.*", vbNormal Or vbArchive Or vbReadOnly Or vbSystem Or vbHidden)

    Do While LenB(F) > 0
    Debug.Print F
    F = Dir$
    Cnt = Cnt + 1
    Loop
    Debug.Print Cnt
    That's an interesting opinion ... Just wonder: do you completely understand, nareth, the difference between using Windows Source (aka Windows API functions) and functions built into VB IDE ? If not then you should ask for a help - there are plenty of resources here and out there.

    Cheers.

  30. #30

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: list files in a folder?

    i know more than you think rhino =)

  31. #31
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: list files in a folder?

    Quote Originally Posted by nareth
    i know more than you think rhino =)
    And I have no doubt about it ...

  32. #32
    Banned
    Join Date
    Dec 2004
    Posts
    12

    Re: list files in a folder?

    banning me is the easy way out.

  33. #33
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: list files in a folder?

    Huh - Is everyone getting banned or what ??
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  34. #34
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: list files in a folder?

    Some other ideas I didn't see mentioned. Either way, you can get the file count and ReDim your array once based on that, rather than using ReDim Preserve.
    (1) Use the FSO and the Count method of the Files collection for the given Folder.
    (2) Use the FileListBox (Visible = False, if desired), and use the ListCount property.
    "It's cold gin time again ..."

    Check out my website here.

  35. #35
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Re: list files in a folder?

    It is with much interest that I tried and studied your code.Since I try
    to understand your code my question is :

    What means LenB ? When I replace the B by any other letter
    I obtain an error message.
    My VB6 books do not mention a Len(B) function.
    Can you explain ?

    Thanks

    Robert XYZ


    Quote Originally Posted by vbasicgirl
    sure, the Dir() function.
    Code:
    Dim F As String
    
    F = Dir$("C:\*.*", vbNormal Or vbArchive Or vbReadOnly Or vbSystem Or vbHidden)
    
    Do While LenB(F) > 0
      Debug.Print F
      F = Dir$
    Loop
    casey.

  36. #36
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: list files in a folder?

    LenB() is a function that gets the length of the variable that is passed to it
    in bytes instead of characters. It is faster then Len. To get the syntax press
    F2 and search for LenB or hit F1 for the definition.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  37. #37
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Re: list files in a folder?

    Thanks for the explanation.

    RobertXYZ


    Quote Originally Posted by RobertXYZ
    It is with much interest that I tried and studied your code.Since I try
    to understand your code my question is :

    What means LenB ? When I replace the B by any other letter
    I obtain an error message.
    My VB6 books do not mention a Len(B) function.
    Can you explain ?

    Thanks

    Robert XYZ

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