Results 1 to 11 of 11

Thread: [RESOLVED] How to get a list of all files and folders in a drive

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Resolved [RESOLVED] How to get a list of all files and folders in a drive

    I am trying to write a program to output a list of all the files and folders in a drive, and I am wondering what methods people would use to achieve this so I can work out which one is fastest and best for my purposes.

    My first attempt, of course, was with the dir() command and I wrote a simple yet effective method of recursive checking (I'm aware dir() doesn't work well with recursive checking, but it doesn't follow a new branch until it has finished in the current folder, and it builds up a list of new folder branches to check) but the problem with using dir() is attributes...if I set it to vbDirectory it lists vbDirectory AND files without any attributes. I am aware that there's a workaround for that where I can do a list of all the files without attributes then of vbDirectory then remove any that are in both lists, but this is a lot of extra work

    The code I wrote for the dir() method:

    Code:
    Private Function getdrive(dr As String) As String
    Do
    drvinf = Dir(dr, vbDirectory)
    Do
    If drvinf <> "" Then
    Debug.Print drvinf 'Used for testing
        AddDir (dr & drvinf)
        drvinf = Dir(, vbDirectory)
        End If
    Loop While drvinf <> ""
    curdircheck = curdircheck + 1
    dr = dirs(curdircheck)
    Loop While curdircheck < dircount
    
    End Function
    Private Function AddDir(direc As String) As String
    dircount = dircount + 1
    dirs(dircount) = direc
    End Function
    As I said above, the problem there is vbDirectory doesn't list JUST the directories, it adds files without any attributes...any better suggestions or ideas for improvement?
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to get a list of all files and folders in a drive

    Are you okay with using FSO?

    If yes then try this
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to get a list of all files and folders in a drive

    There are several CodeBank threads on this topic, so you should be able to find some well optimised methods in there.
    Quote Originally Posted by smUX
    I am aware that there's a workaround for that where I can do a list of all the files without attributes then of vbDirectory then remove any that are in both lists, but this is a lot of extra work
    Indeed it is, a better idea would be to follow the example in the help for Dir:
    Code:
          ' Use bitwise comparison to make sure MyName is a directory.
          If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
    (note that in terms of speed, I think it is better to remove the =vbDirectory part, which doesn't actually affect the behaviour)

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How to get a list of all files and folders in a drive

    Quote Originally Posted by koolsid
    Are you okay with using FSO?

    If yes then try this
    On quick glance, it looks like it'll do the job, although it is a bit slow and processor-intensive (although the drive I am checking IS 500GB, so it will be slow to check :-)).

    I'm happy trying any suggestions, whatever it entails...I want to find the fastest one for my requirements :-)

    Any idea if this can handle hidden files/folders?

    And Si, thanks...that SHOULD be a very useful suggestion...would make a great deal of difference. I could in theory add that to the section which adds new directories found and it only adds it to the array IF the link is indeed a directory...exactly what I was after :-)

    Edit: Si, I've made the changes you suggested (mixed with what I just said) and it works although at the moment there's a few bugs. When I have a fully working function I'll post it here so people can help improve it and so others can use it
    Last edited by smUX; Nov 11th, 2008 at 11:45 AM.
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to get a list of all files and folders in a drive

    note: on repeat testing the process seems to work faster than on an initial test, which may be to do with caching

    i did some test comparison in a thread in office development forum using both DIR and FSO
    there is also api method to do the same, i think posted by vbasicgirl, but i don't really believe there is a lot of difference no matter which you use
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to get a list of all files and folders in a drive


  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to get a list of all files and folders in a drive

    funny how i keep trying to reinvent the wheel
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How to get a list of all files and folders in a drive

    Many people reinvented the wheel...Dunlop, for one...many people succeeded to improve on the original :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How to get a list of all files and folders in a drive

    Quote Originally Posted by westconn1
    note: on repeat testing the process seems to work faster than on an initial test, which may be to do with caching
    Trouble is that if you are talking about FSO, I only need the full directory scan to be done once, although it will regularly update the tree so maybe it'll work okay for me...however, I'm working with DIR for the moment, to see if it's enough for my needs; it does seem capable of doing the job required of it, and in the IDE it takes no more than a minute or so to get a full 500GB hard drive of folders (over 5000 folders).

    DIR is the simplest method and doesn't require any other dependants, although it isn't important to be non-dependant, just useful
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to get a list of all files and folders in a drive

    i never see any reason to use FSO except for scripting, though many other people prefer to use it, it is only a matter of choice

    the point about taking longer on initial test, than subsequent ones, was mainly about speed testing, rather than actual use once the program is completed
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: [RESOLVED] How to get a list of all files and folders in a drive

    I've marked this resolved although I am still working on it. I have a fully working folder grabber and it is a simple matter to write in a recursive file grabber.

    The code I have also uses custom compression to reduce the amount of memory (and hard drive space for the moment, it stores the folders to a file) down to 1/4 of its original size by referencing previous folders that have the same info (for instance, dirs(1) has "c:\" so if dirs(2) is "c:\windows\" it replaces the "c:\" with a 2 byte value which resolves to the number 1 (referencing dirs(1))...then if you had "c:\windows\system32" it would reference dirs(2) rather than keep the full location). When I have the file grabbing included and the compressed data all working, I will post both the compressing version and the basic version...could be a while though, I am a lazy and slow programmer :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

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