ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   comparison of 2 approaches for identifying file existence (https://www.excelbanter.com/excel-programming/421320-comparison-2-approaches-identifying-file-existence.html)

broro183[_2_]

comparison of 2 approaches for identifying file existence
 
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...


Per Jessen

comparison of 2 approaches for identifying file existence
 
Hi Rob

Why not use the FolderExists method?

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

Regards,
Per

"broro183" skrev i meddelelsen
...
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...



broro183[_2_]

comparison of 2 approaches for identifying file existence
 
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/.../msg01857.html
as I've recently mentioned in post # 2 of
http://www.excelforum.com/excel-prog...pen-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...


"Per Jessen" wrote:

Hi Rob

Why not use the FolderExists method?

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

Regards,
Per

"broro183" skrev i meddelelsen
...
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...




Per Jessen

comparison of 2 approaches for identifying file existence
 
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

"broro183" skrev i meddelelsen
...
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/.../msg01857.html
as I've recently mentioned in post # 2 of
http://www.excelforum.com/excel-prog...pen-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...


"Per Jessen" wrote:

Hi Rob

Why not use the FolderExists method?

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

Regards,
Per

"broro183" skrev i meddelelsen
...
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...





Peter T

comparison of 2 approaches for identifying file existence
 
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


"broro183" wrote in message
...
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...




Dave Peterson

comparison of 2 approaches for identifying file existence
 
One mo

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.

broro183 wrote:

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...


--

Dave Peterson

broro183[_2_]

comparison of 2 approaches for identifying file existence
 
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...


All times are GMT +1. The time now is 11:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com