[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? :)
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
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)
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
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
Re: How to get a list of all files and folders in a drive
Re: How to get a list of all files and folders in a drive
funny how i keep trying to reinvent the wheel
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 :-)
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 :)
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
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 :-)