I would like to know the code to do a “if file exist then” in visual basic.

1,940 pts.
Tags:
.bat file
Visual Basic
Visual Basic developers
Visual Basic Functions
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.

ASKED: June 30, 2010  6:18 AM
UPDATED: July 1, 2010  6:29 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question: 8  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TheFinder
    I still don't understand. where do I put the file name, etc.
    1,940 pointsBadges:
    report
  • Vatchy
    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 pointsBadges:
    report
  • TheFinder
    Where do I put the Private function at, declaratons? or on it's on page?
    1,940 pointsBadges:
    report
  • TheFinder
    Ok. I've done as you stated but to no avail. Please clarify if you will. Thank you.
    1,940 pointsBadges:
    report
  • carlosdl
    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).
    65,110 pointsBadges:
    report
  • Vatchy
    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 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following