Dir versus FSO

mvidas
mvidas used Ask the Experts™
on
Hi Everyone,

In regards to using the Dir command and using FileSystemObject, I'm curious as to what people think is better.  I used to use dir for everything, until about a year ago when someone pointed out to me (I can't find the thread) that FSO is more efficient.  I've been using that for everything, until I read PaulHews' comment at http:Q_21347422.html#13519390 .  Since I trust his opinion, it got me to think that I'm not writing as good code as I could be.  Personally I think that FSO is easier to understand, and easier to recurse subdirectories, but that could be because I use it more.

What are people's thoughts on this?

Matt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Steve SiricaSenior Web Application Engineer
Commented:
dir has a smaller foot print but I think FSO is more powerfull and flexible.
2nd Line Desktop Support
Commented:
I agree with that but also would suggest looking into the API's with regards to finding files ie FindFirstFile() FindNextFile() API's I think it is something like that :)

Take a look here :

http://vbnet.mvps.org/index.html?code/fileapi/countfiles.htm

That explains it and compares API Versus FSO and other things :) I hope this helps
Commented:
Time to revive PAFSO
http:Q_11499199.html
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
Dir is useless for recursive searches, but to iterate through files in a single directory, it doesn't make sense to load up the fso.
Thanks all, good information so far. I've got a bit of reading to do on it, but from what I read so far looks like I might become a member of pafso :)
Shane Russell2nd Line Desktop Support

Commented:
how do you become a member of pafso , I click on the links and it comes with an error page saying that it does not exist or something to that extent :) I must be doing something wrong h03h03h03h03
Commented:
@paulhews; dir is easy to recurse. as you know, it doesnt maintin recursive state so you need to store the paths you wish to investigate in an array, something like this:


public sub recurseDir(path as String)

  If path = "" Then Exit Sub

  Dim dirs as String
  Dim toInvestigate() as String

  Dim file as String
  file = Dir(path, vbNormal)
  While file <> ""
    Debug.Print path & "\" & file
  Wend

  file = Dir(path, vbDirectory)
  While  file <> ""
    dirs = path & "\" & file & "*"
  Wend

  toInvestigate = Split(dirs, *)
  Dim i as Integer
  For i = 0 to UBound(toInvestigate)
    recurseDir(toInvestigate(i))
  Next i
End Sub



the dirs array is separated by *, something not allowed in file names, perhaps better options exist, like Chr(0)
the trailign * on dirs ensures that the last element of toInvestigate is actually a blank string ""
this terminated the recursion
Sorry it took so long to get this closed, I've been playing with the APIs, though want to play a bit more before I start using it regularly.

Thanks everyone!  As this was more of an opinion question, I'm splitting the points equally.
Matt
Shane Russell2nd Line Desktop Support

Commented:
Glad we managed to help you out :) and thank you for the grade and points :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial