PC Review


Reply
Thread Tools Rate Thread

comparison of 2 approaches for identifying file existence

 
 
broro183
Guest
Posts: n/a
 
      15th Dec 2008
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...

 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      15th Dec 2008
Hi Rob

Why not use the FolderExists method?

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

Regards,
Per

"broro183" <(E-Mail Removed)> skrev i meddelelsen
news:B6465D5E-EB11-4B2A-B786-(E-Mail Removed)...
> 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...
>


 
Reply With Quote
 
broro183
Guest
Posts: n/a
 
      15th Dec 2008
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" <(E-Mail Removed)> skrev i meddelelsen
> news:B6465D5E-EB11-4B2A-B786-(E-Mail Removed)...
> > 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...
> >

>
>

 
Reply With Quote
 
Per Jessen
Guest
Posts: n/a
 
      15th Dec 2008
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" <(E-Mail Removed)> skrev i meddelelsen
news:8792F98D-276D-407F-B285-(E-Mail Removed)...
> 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" <(E-Mail Removed)> skrev i meddelelsen
>> news:B6465D5E-EB11-4B2A-B786-(E-Mail Removed)...
>> > 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...
>> >

>>
>>


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      15th Dec 2008
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" <(E-Mail Removed)> wrote in message
news:B6465D5E-EB11-4B2A-B786-(E-Mail Removed)...
> 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...
>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      15th Dec 2008
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.

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
 
Reply With Quote
 
broro183
Guest
Posts: n/a
 
      16th Dec 2008
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...
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.xls file dumped out of existence PhilNotVeryHappy Windows Vista File Management 4 8th Feb 2008 04:46 AM
Check for existence of a file =?Utf-8?B?REtT?= Microsoft Excel Programming 4 9th Apr 2007 08:46 PM
Existence of a file Jaime Lucci Microsoft VB .NET 5 28th Oct 2005 08:42 PM
test for existence of file DC Gringo Microsoft VB .NET 12 20th Sep 2004 11:26 PM
Re: test for existence of file DC Gringo Microsoft VB .NET 1 18th Sep 2004 05:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:17 AM.