14. VbScript | How to Check if File & Folder Exists

So you can use VbScript to move and copy files around on your computer. But if the specific file or folder does not exist, you are going to get an error. So in this tutorial I am going to show you how to check before this error occurs. First we need to start with a New CreateObject Command:

CreateObject("Scripting.FileSystemObject")

Add to the end of .FileExists or .FolderExists command:

CreateObject("Scripting.FileSystemObject").FileExists 'checks for the existence of file
CreateObject("Scripting.FileSystemObject").FolderExists 'checks for the existence of folder

Let’s streamline the VbScript:

Option Explicit
Dim fso 
Set fso = CreateObject("Scripting.FileSystemObject")
fso.FileExists("")

Choose an image from your Desktop:

desktop_cat_icon

Right Click and choose Properties. From there copy the Location:

a2

Paste Location into fso.FileExist(” “) with the filename of the picture:

Option Explicit
Dim fso 
Set fso = CreateObject("Scripting.FileSystemObject")
fso.FileExists("C:\Users\rock\Desktop\BigEyeCat.jpg")

Now we need to make a Feedback Loop that tells us whether the file exists or not:

Option Explicit
Dim fso 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\Users\rock\Desktop\BigEyeCat.jpg") Then 
wscript.echo "yes"
Else
wscript.echo "no"
End If

Output:

a3

The Feedback Loop that tells us whether the folder exists or not:

Dim fso 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists("C:\Users\rock\Desktop\testfolder") Then 
wscript.echo "yes"
Else
wscript.echo "no"
End If

What's Your Opinion?