eHow launches Android app: Get the best of eHow on the go.

How To

How to Check That a File Exists Using VBScript

Member
By who10
User-Submitted Article
(0 Ratings)
Information Technology
Information Technology
John C. Hooten

At times, when programming with VBScript, a programmer will want to know if a file exists prior to performing another function.

This is good programming practice and it can be accomplished by accessing the Windows Script Host via VBScript and making use of the FileSystemObject.

The program below will walk you through the steps necessary to accomplish this task and give you a real world example to learn from.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • Computer
  • Windows(98 or Newer)
  1. Step 1

    Create a file

    Create a file on your computer named test.txt at the following location C:\Temp\test.txt

    This will be used to demonstrate the VBScript.

  2. Step 2

    Open Notepad

    Go to Start - All Programs - Accessories - Notepad
    Save your file as C:\Temp\FileCheck.vbs

    Note: Adding .vbs to any .txt file creates an executable VBScript file.

  3. Step 3

    Copy Code

    Copy and Paste the following code into Notepad.

    '**************************************************************
    Option Explicit

    'Set Dimension
    DIM fso

    'Set Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    'Create Condition
    If (fso.FileExists("C:\Temp\test.txt")) Then
    'Alert User
    WScript.Echo("File exists!")
    WScript.Quit()
    Else
    'Alert User
    WScript.Echo("File does not exist!")
    End If

    'Exit Script
    WScript.Quit()
    '**************************************************************
    Save your VBScript file and exit Notepad

  4. Step 4
    File exists!
     
    File exists!

    4.Run Script

    Browse to C:\Temp\FileCheck.vbs using My Computer or Explore, depending on what you're use to.
    Double-click on VBScript file "FileCheck.vbs". You should get a message box as shown below stating that the "File exists!". Select "OK"

  5. Step 5
    File does not exist!
     
    File does not exist!

    Now rename or remove the following file C:\Temp\test.txt and Double-click on the FileCheck.vbs again.
    This will create a condition where the file does not exist and you should get a message box which states "File does not exist!" Select "OK"


    The If Then Else statement in the code above is doing the check and alerting you as to whether or not the file exists.

    Congratulations! You've written a VBScript to check for the existance of a file.

Tips & Warnings
  • When working with vbscript(.vbs) files you'll want to right-click on them and choose Edit to modify them, otherwise you will run the script.

Comments  

who10 said

Flag This Comment

on 6/30/2009 I would do something like this.

'******************************************************
Option Explicit

'add
Const EVENT_SUCCESS = 0

'Set Dimension
DIM fso

'add
DIM objShell

'Set Object
Set fso = CreateObject("Scripting.FileSystemObject")

'add
Set objShell = wscript.CreateObject("Wscript.Shell")

'Create Condition
If (fso.FileExists("C:\Temp\test.txt")) Then

'add
objShell.LogEvent EVENT_SUCCESS, _
"File exists!"

'Alert User
WScript.Echo("File exists!")
WScript.Quit()
Else
'Alert User
WScript.Echo("File does not exist!")
End If

'Exit Script
WScript.Quit()
'******************************************************

Check out http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

choat1 said

Flag This Comment

on 6/23/2009 What would I need to do to get this code to post an event to the Applciation Eventlog on Windows?

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics