Visual basic 6 - file existance

Miscellaneous Forums/General Discussion/Visual basic 6 - file existance

Hi, does anyone know the most simple way i can check is i file exists in a directory in VB6? i'm trying to make a validation routine for saving a file, so when the user clicks save the program will check is the file already exists and then respond accordingly. thanks.

http://www.devx.com/vb2themax/Tip/18942

Hi, i tried the code from that page, but it doesn't seem to work for me, i changed the variables accordingly to my project, could be that i'm not understanding the code properly, could you please explain the concept behind the code. thanks

Doesn't FileSystemObject have a FileExists() method? Its been years since I used VB6 but I'm quite sure it does, i.e.

Dim fso As New FileSystemObject
If fso.FileExists("file")
  ...whatever
EndIf


the filesystemobject doesn't seem to be recognised as a variable type for some reason, i've looked in the msdn for it and it does have information on it but it still doesn't work, any ideas why? thanks

Try this.

Dim l_FS As Object

Set l_FS = CreateObject("Scripting.FileSystemObject")


(edit - I forgot this line) l_FS.FileExists("file")

And without the FileSystemObject:
FileExists=Dir$(FileName$)<>""


I use a lot of code from this site:

http://vbnet.mvps.org/

Lots of little functions for IO stuff

Is it possible to do by checking file size?

I don't know if this work or not though...

Is it possible to do by checking file size?
No, because a file can exist with a size of 0 bytes.

Googleed some results

http://www.vbforums.com/archive/index.php/t-349990.html

http://www.freevbcode.com/ShowCode.asp?ID=346

http://www.pbdr.com/vbtips/gen/fileexst.htm

If you use size check, -1 may present as "not exist" or "error", handle with care.

> Is it possible to do by checking file size?

Why would you want to though? The proper method of checking for file existance is easy, well once you get past the incorrect documentation.

This habit actually came from using PureBasic... check the file size return -1 if not exist, -2 if directory, I don't know if it applies to other languages