comparison of 2 approaches for identifying file existence

B

broro183

hi all,

I have come across the below techniques for identifying if a file/folder
exists, both seem to work but is one technique "better" than the other?

if not dir(strfullpath,vbdirectory) = vbnullstring then doesfilefolderexist
= true
'or
if len(dir(sbasefolder, vbdirectory)) > 0 then doesfilefolderexist = true

TIA
Rob
__________________
Rob Brockett
NZ
Always learning & the best way to learn is to experience...
 
P

Per Jessen

Hi Rob

Why not use the FolderExists method?

Set fs = CreateObject("Scripting.FileSystemObject")
fe = fs.FolderExists(strfullpath)

Regards,
Per
 
B

broro183

hi Per,

A third option eh?

I haven't seen this option before but I am wary of using FSO approaches
after reading the below link:
http://www.tech-archive.net/Archive/VB/microsoft.public.vb.general.discussion/2007-08/msg01857.html
as I've recently mentioned in post # 2 of
http://www.excelforum.com/excel-pro...to-the-filecopy-statement-for-open-files.html

When you line the FSO approach up against my suggestions, is one technique
"better" than the others?

Thanks
Rob

__________________
Rob Brockett
NZ
Always learning & the best way to learn is to experience...
 
P

Per Jessen

Hi again,

I think I would prefer my method, but of course it depends on what shall
happen later in the code.

If the folder should be created when fe=false, or if you need to check if
file exists when fe= true using fs.FileExiste(MyFolder & MyFileName) I would
use this method for sure.

Hopes it helps

Regards,
Per
 
P

Peter T

In effect both methods are (virtually) the same, albeit one does a string
comparison and the other checks the length of the string returned by Dir. If
I had to choose I'd opt for the Len() but not much in it.

Dir works differently for files and folders, but the function as written
will not distinguish (will return true if the returned string is a file or a
folder)

I have read reports that Dir can be buggy though I have never found it to be
so. If it is it might be due to not having been "reset" for some reason or
other, which you can do with the following before you use it
Call Dir("nul")

AFAIK the Dir method is fine but FWIW I check for files & folders like this

Function FileOrFolder(s As String) As Long
' File exists : +1
' Folder exists : -1
' Neither file nor folder : 0

Dim nAttr As Long
On Error Resume Next
nAttr = GetAttr(s)

If Err.Number = 0 Then
' include the brackets
If (nAttr And VBA.vbDirectory) = 0 Then
FileOrFolder = 1 ' file
Else
FileOrFolder = -1 ' folder
End If
End If

End Function


There is also the Scripting method as suggested by Per Jessen (use
fs.FileExists for files). Some administrators disable scripting so if not
sure start with something like this

Dim fso As Object
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
If fso Is Nothing Then
'use some other method

The first time the fso is created can be a little slow in some systems.

Regards,
Peter T
 
D

Dave Peterson

One more:

Dim TestStr as string
TestStr = ""
on error resume next
teststr = dir("c:\the folder\nul")
on error goto 0

if teststr = "" then
'doesn't exist
else
'exists
end if

Nul is an old DOS device (like PRN, CON, AUX, LPT#, ...) that exists for each
folder.
 
B

broro183

Thanks for the input everyone :)

I'll check out if I can use scripting & test it on the profiles of another
couple of users as well.
I haven't seen "GetAttr(s)" before so I may well make use of that in some of
my coding.

Everyone has offered something towards answering my general question so I'm
ticking "yes" for each post - I hope that that's okay...


Thanks
Rob
__________________
Rob Brockett
NZ
Always learning & the best way to learn is to experience...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top