Things You'll Need:
- Computer
- Windows(98 or Newer)
-
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. -
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. -
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 -
Step 4
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" -
Step 5
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.














Comments
who10 said
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
on 6/23/2009 What would I need to do to get this code to post an event to the Applciation Eventlog on Windows?