1,940 pts.
 I would like to know the code to do a “if file exist then” in visual basic.
for some reason the first time I posted this it only showd some of it, so I've reposted it.

I would like to know the code to do a "if file exist then" in visual basic. something like in a .bat file as follows. IF EXIST C:install.log ( @echo I'm here ) ELSE ( @echo Your File is missing. ) @rem @rem pause I know how to do an on Error go to but that is not what I'm wanting to do here. I just want to check if a certain file exists example If “C:remote directoryfile.txt” exist then goto that line. Any help would be greatly appreciated.



Software/Hardware used:
ASKED: June 30, 2010  6:18 AM
UPDATED: July 1, 2010  6:29 PM

Answer Wiki:
here is a VBA example, that should be of use. Function DoesFileExist(FileSpec) As Boolean ' See if a file exists Dim FSO As FileSystemObject Set FSO = CreateObject("Scripting.FileSystemObject") If FSO.FileExists(FileSpec) Then DoesFileExist = True Else DoesFileExist = False End If End Function
Last Wiki Answer Submitted:  June 30, 2010  1:21 pm  by  SidSeton   95 pts.
All Answer Wiki Contributors:  SidSeton   95 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

I still don’t understand. where do I put the file name, etc.

 1,940 pts.

 

Try this function:

Private Function Check_File(sPath As String) As Boolean
    If Dir$(sPath) <> "" Then
        Check_File = True
    Else
        Check_File = False
    End If
End Function

Call it this way:

Dim bFileExists as Boolean
If bFileExists(your_filename) = True Then...
 1,410 pts.

 

Where do I put the Private function at, declaratons? or on it’s on page?

 1,940 pts.

 

Ok. I’ve done as you stated but to no avail. Please clarify if you will. Thank you.

 1,940 pts.

 

Hi TheFinder.

Actually, you should call the function provided by Vatchy this way:

If Check_File(<your_filename>) = True Then

(Replacing <your_fllename> with the name of the file you want to check).

 63,535 pts.

 

Sorry, major brain fart. Carlosdl is correct about how to call it. Replace the last two lines I gave you (Dim and If) and replace with his example.

 1,410 pts.

 

Thank you, it’s now working, but is there a way to have it check and not show the file and path to file in a msgbox before it replies to true or false.

 1,940 pts.

 

Thank you both Vatchy and Carlosdl,
It was my bad, yesterday on debugging I had told it to msgbox me and today I forgot that line was there, it’s now working as expected. you rock.

 1,940 pts.