vbscript Checking for the existence of a file/folder/drive


Methods used:

.DriveExists(strDrive) returns (True/False)
.FileExists(strFile) returns (True/False)
.FolderExists(strFolder) returns (True/False)

The following code checks for the existence of a file using the "FileExists" method of a file system object. For checking the existence of Folder or a drive, one can use the method "FolderExists" or "DriveExists" respectively.


Dim strPath, objFso
strPath = "C:\Users\GS\Desktop\tasks.txt"        'Enter the absolute path of the File/Folder/Drive
Set objFso = CreateObject("Scripting.FileSystemObject")

'Checking for the File's existence
If objFso.FileExists(strPath) then               'returns True if the file exists, else False
    Msgbox "File Exists!"
    Msgbox "File does not Exist!"
End If
Set objFso = Nothing