Friday, November 18, 2005

See if Files, Folders and Drives Exist

These functions will allow to determine whether files, folders and drives exist at runtime.

Code to see whether a file exists:
Public Function FExists(OrigFile As String)
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
FExists = fs.fileexists(OrigFile)
End Function
'Returns a boolean - True if the file exists


Code to see whether a folder exists:
Public Function DirExists(OrigFile As String)
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
DirExists = fs.folderexists(OrigFile)
End Function
'Returns a boolean - True if the folder exists


Code to check the state of a drive (returns 0 if the drive does not exist, 1 if the drive exists but contains no media, 2 if the drive exists and contains media. Hard-drives will always return 2):
Public Function DExists(OrigFile As String)
Dim fs, d
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.driveexists(OrigFile) = True Then
Set d = fs.getdrive(OrigFile)
DExists = 1
If d.isready = True Then
DExists = 2
Exit Function
End If
Else
DExists = 0
End If
End Function

No comments:

Digg it !