Newbie Question: File existence checking 
Author Message
 Newbie Question: File existence checking

I am trying to figure out how to initially determine the existence of
config file I will be using.  If the file exists, then the configuration
will be loaded, if it doesn't exist, a new array structure will be created,
then saved.  I've searched the docs for information on files, however I have
yet to find a function/method, etc. that either returns False or NULL if a
file doesn't exist, or wether or not the open command will return such
information.

While I'm no stranger to programming (5 years of C, plus a multitude of
other languages) I just started with Visual Basic a few days ago.  Any help
would be appreciated.  Especially if it also contains pointers to
appropriate documentation in either the on-line docs, or the VB5
Programmer's Guide.  Thanks for any information.

Paul Ryan Kuykendall

-----
50. My main computers will have their own special operating system that will
be completely incompatible with standard IBM and Macintosh powerbooks.

From - The Top 100 Things I'd Do If I Ever Became An Evil Overlord



Fri, 20 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking

Dear Paul,

Yes, the only way I found to check for the existance of a file is to error
out and catch the error on on_error routine.

I close a program automatically if the timer routine does not find a file.

Perhaps this code will help you.

-----------------------start code--------------------------------------

Private Sub Timer1_Timer()
On Error GoTo errortrapLOAD
  Open "\\Sthelens\programs\DONTQUIT.TXT" For Input As #100
  Close #100
      Exit Sub
errortrapLOAD:

End

End Sub

------------------------------end Code----------------------------------

Quote:

>I am trying to figure out how to initially determine the existence of
>config file I will be using.  If the file exists, then the configuration
>will be loaded, if it doesn't exist, a new array structure will be created,
>then saved.  I've searched the docs for information on files, however I
have
>yet to find a function/method, etc. that either returns False or NULL if a
>file doesn't exist, or wether or not the open command will return such
>information.

>While I'm no stranger to programming (5 years of C, plus a multitude of
>other languages) I just started with Visual Basic a few days ago.  Any help
>would be appreciated.  Especially if it also contains pointers to
>appropriate documentation in either the on-line docs, or the VB5
>Programmer's Guide.  Thanks for any information.

>Paul Ryan Kuykendall

>-----
>50. My main computers will have their own special operating system that
will
>be completely incompatible with standard IBM and Macintosh powerbooks.

>From - The Top 100 Things I'd Do If I Ever Became An Evil Overlord



Sat, 21 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking

No, No, No.

To check if a file exists,

if dir("FilePath\file.ext") <> "" then
        ' File Exists
endif

--
Lee Cooper
Cooper Computer Services Ltd


Quote:
> Yes, the only way I found to check for the existance of a file is to
error
> out and catch the error on on_error routine.

> >I am trying to figure out how to initially determine the existence of
> >config file I will be using.  If the file exists, then the configuration



Sat, 21 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking

: No, No, No.

: To check if a file exists,

: if dir("FilePath\file.ext") <> "" then
:       ' File Exists
: endif

: --
: Lee Cooper
: Cooper Computer Services Ltd


: > Yes, the only way I found to check for the existance of a file is to
: error
: > out and catch the error on on_error routine.

: > >I am trying to figure out how to initially determine the existence of
: > >config file I will be using.  If the file exists, then the configuration

Well, not quite.  This (Dir$) will cause an error if the PATH doesn't
exist (error 76, "Path not found").  Dir$ is good for lots of things, but
I don't know any way to completely avoid an error trap.

You might prefer to localize the error handling with a wrapper function
that contains the error handling, something like:

Function bFileExists(sFilePathName as string) as boolean
Dim s as string
  On Error Resume Next
  s = Dir$(sFilePathName)
  If Err = 0 And Len(s) <> 0 Then
    ' ok if no error and Dir$ finds something
    bFileExists = True
  Else
    bFileExists = False
  Endif
Exit Function

HTH -

--



Sat, 21 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking


Quote:
>Thanks for the note, but I don't get an error with the Dir command.

>I also use Dir() rather than Dir$ as Basic is (thankfully) moving away from
>type-casting variables.

<cut>

VB5 does not return an error if the path does not exist - VB3 did (I don't
remember about VB4).  It's smart to have an error trap around it anyway.

The Dir() function returns a variant containing a string.  The Dir$()
function returns a string.  Moving away from type-casting is nothing to be
thankful for as it simply takes more memory and slows things down.  If
anything, VB needs to implement more and stronger type casting and checking.
Use Dir$ unless you have a specific reason for needing a variant.



Sat, 21 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking

Lee

Read the original posting. The author wishes to know whether his
configuration file exists. If it does, then he wants to read it,
otherwise create a new internal array. Therefore, it does him no good
using Dir$ because he's still got to open the file. The 'correct' way is
to open the file and monitor the errors.

Regards
{*filter*}


Quote:
>No, No, No.

>To check if a file exists,

>if dir("FilePath\file.ext") <> "" then
> ' File Exists
>endif

>--
>Lee Cooper
>Cooper Computer Services Ltd


>> Yes, the only way I found to check for the existance of a file is to
>error
>> out and catch the error on on_error routine.



- Show quoted text -

Quote:
>> >I am trying to figure out how to initially determine the existence
of
>> >config file I will be using.  If the file exists, then the
configuration



Sat, 21 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking

Hi Paul

Guess Dir function should work for that.
if(Dir(Path name,Filename/*.Ext) = "")
      File not found
end if
Courtesy: Online help



Sat, 21 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking

I check for the existence of files by calling this routine, which I include
in my programs:

Function Exists(PathFileName as String) as Boolean
        On Error Goto ErrorHandler
        Exists = False
        If Dir(PathFileName) <> "" Then
                Exists = True
        EndIf
        Exit Function
ErrorHandler:
End Function

Note that the built-in error routine is necessary, because if a
non-existent path is included with the filename, an error is generated by
the DIR() function, which could terminate your program unless handled.

- Warren L. McFerran



Quote:
> I am trying to figure out how to initially determine the existence of
> config file I will be using.  If the file exists, then the configuration
> will be loaded, if it doesn't exist, a new array structure will be
created,
> then saved.  I've searched the docs for information on files, however I
have
> yet to find a function/method, etc. that either returns False or NULL if
a
> file doesn't exist, or wether or not the open command will return such
> information.

> While I'm no stranger to programming (5 years of C, plus a multitude of
> other languages) I just started with Visual Basic a few days ago.  Any
help
> would be appreciated.  Especially if it also contains pointers to
> appropriate documentation in either the on-line docs, or the VB5
> Programmer's Guide.  Thanks for any information.

> Paul Ryan Kuykendall

> -----
> 50. My main computers will have their own special operating system that
will
> be completely incompatible with standard IBM and Macintosh powerbooks.

> From - The Top 100 Things I'd Do If I Ever Became An Evil Overlord



Sat, 21 Oct 2000 03:00:00 GMT  
 Newbie Question: File existence checking

Thanks to everybody who helped.  I think I've got that problem solved.  It's
just a little strange going from Unix programming in C to Windows
programming in VB.  Anyway, thanks.

Paul Kuykendall



Sun, 22 Oct 2000 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Newbie - Check for file existence

2. A2K Check for Existence of Back End File

3. Checking for the existence of a file

4. How to check for existence of a file on harddisk form OL VBscript

5. Check File Existence?

6. Checking for the Existence of a File

7. Checking for random file existence in Visual Basic

8. Checking for existence of a file???

9. Check for existence of a file?

10. Checking for existence of a file...

11. Check for the existence of a file.

12. Checking the Existence of a File

 

 
Powered by phpBB® Forum Software