Is there an efficient way of checking if a file exists? =)
If so please explain. I've always been poor in this area, and always done it the cheap and unefficient way. Hehehe
Thanks :)
Printable View
Is there an efficient way of checking if a file exists? =)
If so please explain. I've always been poor in this area, and always done it the cheap and unefficient way. Hehehe
Thanks :)
VB Code:
If Len(Dir$("C:\myFile.txt")) > 0 Then MsgBox "File Exists" Else MsgBox "File Not Found" End If
:)
you could drop the Len() couldn't you? Dir() works just fine.
Hi
You could also check for error numbers when opening a file using Err.Number ... this allows u to check for all file problems such as not existing, already open, no directory etc etc etc etc... look up Err.Number in help for all of the possible checks.
Regards
Stuart
crptcblade is spot on, checking the length of returned value from dir is much faster than doing a string comparison and is therefore the optimised approach.