For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > April 2005 > FileExists









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author FileExists
rasinc

2005-04-26, 4:04 am

I must be brain fried tonight. I am trying to test to see if a file exists
before I attempt to open it. The help I finally found indicates
object.FileExists(filespec). However, I just can't figure out what object?

I have the path and filename in a variable and just want to see if it exists
before I open it. I will also want to run a routine that will check a series
of files stored as paths and filenames in a database to see if they exist all
at the same time.

eg.

If FileExists(file) then
do some stuff
endif

Any help is appreciated.
Ed

2005-04-26, 4:04 am

On Mon, 25 Apr 2005 21:37:03 -0700, rasinc
<rasinc@discussions.microsoft.com> wrote:

>I must be brain fried tonight. I am trying to test to see if a file exists
>before I attempt to open it. The help I finally found indicates
>object.FileExists(filespec). However, I just can't figure out what object?
>
>I have the path and filename in a variable and just want to see if it exists
>before I open it. I will also want to run a routine that will check a series
>of files stored as paths and filenames in a database to see if they exist all
>at the same time.
>
>eg.
>
>If FileExists(file) then
> do some stuff
>endif
>
>Any help is appreciated.


I use this to detect if a file exists already....

Public Function Exists%(F$)
On Error Resume Next
X& = FileLen(F$)
If X& Then Exists% = True
End Function

If Exists("C:\myfile.ext") Then
' DoStuff
End If

Ed

Tony Proctor

2005-04-26, 4:03 pm

Although I welcome any alternatives that avoid the FSO (File System
Object -- I think this is what the OP read about), I'm afraid your sample
code will fail to detect whether a zero-length file exists.

The following are generally considered much safer:

Public Function bFileExists(sFile As String) As Boolean
' Tests for file existence
On Error Resume Next
bFileExists = ((GetAttr(sFile) And vbDirectory) = 0)
End Function

Public Function bDirExists(sDir As String) As Boolean
' Tests for directory existence
On Error Resume Next
bDirExists = ((GetAttr(sDir) And vbDirectory) <> 0)
End Function


Tony Proctor

"Ed" <eat@joes.com> wrote in message
news:62ir61hf873v30hm19sdohogvu7q2d70kk@
4ax.com...
> On Mon, 25 Apr 2005 21:37:03 -0700, rasinc
> <rasinc@discussions.microsoft.com> wrote:
>
exists[color=darkred]
object?[color=darkred]
exists[color=darkred]
series[color=darkred]
all[color=darkred]
>
> I use this to detect if a file exists already....
>
> Public Function Exists%(F$)
> On Error Resume Next
> X& = FileLen(F$)
> If X& Then Exists% = True
> End Function
>
> If Exists("C:\myfile.ext") Then
> ' DoStuff
> End If
>
> Ed
>



rasinc

2005-04-27, 4:00 am

Thanks. Seems to work great.

"Tony Proctor" wrote:

> Although I welcome any alternatives that avoid the FSO (File System
> Object -- I think this is what the OP read about), I'm afraid your sample
> code will fail to detect whether a zero-length file exists.
>
> The following are generally considered much safer:
>
> Public Function bFileExists(sFile As String) As Boolean
> ' Tests for file existence
> On Error Resume Next
> bFileExists = ((GetAttr(sFile) And vbDirectory) = 0)
> End Function
>
> Public Function bDirExists(sDir As String) As Boolean
> ' Tests for directory existence
> On Error Resume Next
> bDirExists = ((GetAttr(sDir) And vbDirectory) <> 0)
> End Function
>
>
> Tony Proctor
>
> "Ed" <eat@joes.com> wrote in message
> news:62ir61hf873v30hm19sdohogvu7q2d70kk@
4ax.com...
> exists
> object?
> exists
> series
> all
>
>
>

Randy Birch

2005-04-27, 4:00 am

Just fyi, that "object" referred to is the file sysetm object. You don't
want to use that.

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/
----------------------------------------------------------------------------



"rasinc" <rasinc@discussions.microsoft.com> wrote in message
news:06B03C77-B281-45E0-BBAB-F6222D3AB064@microsoft.com...
:I must be brain fried tonight. I am trying to test to see if a file
exists
: before I attempt to open it. The help I finally found indicates
: object.FileExists(filespec). However, I just can't figure out what
object?
:
: I have the path and filename in a variable and just want to see if it
exists
: before I open it. I will also want to run a routine that will check a
series
: of files stored as paths and filenames in a database to see if they exist
all
: at the same time.
:
: eg.
:
: If FileExists(file) then
: do some stuff
: endif
:
: Any help is appreciated.

rasinc

2005-04-30, 4:02 am

Thanks. Tony's code works well. It has trapped all testing I have done so
far.

"Randy Birch" wrote:

> Just fyi, that "object" referred to is the file sysetm object. You don't
> want to use that.
>
> --
>
> Randy Birch
> MS MVP Visual Basic
> http://vbnet.mvps.org/
> ----------------------------------------------------------------------------
> Read. Decide. Sign the petition to Microsoft.
> http://classicvb.org/petition/
> ----------------------------------------------------------------------------
>
>
>
> "rasinc" <rasinc@discussions.microsoft.com> wrote in message
> news:06B03C77-B281-45E0-BBAB-F6222D3AB064@microsoft.com...
> :I must be brain fried tonight. I am trying to test to see if a file
> exists
> : before I attempt to open it. The help I finally found indicates
> : object.FileExists(filespec). However, I just can't figure out what
> object?
> :
> : I have the path and filename in a variable and just want to see if it
> exists
> : before I open it. I will also want to run a routine that will check a
> series
> : of files stored as paths and filenames in a database to see if they exist
> all
> : at the same time.
> :
> : eg.
> :
> : If FileExists(file) then
> : do some stuff
> : endif
> :
> : Any help is appreciated.
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com