Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

File, Folder, Drive Exists

From Wiki

Jump to: navigation, search

Here are three functions to determine whether a file, folder or drive exists using the FileSystemObject. It is also possible to use Dir to determine if a file or folder exists, however, Dir will return nothing or an error (Runtime Error 52) if you try to check a UNC path.

Note that

  FolderExists("C:")

Will return True.

File Exists

  Function FileExists(FilePath As String) As Boolean
  'The reference for the FileSystemObject Object is Windows Script Host Object Model
  'but it not necessary to add the reference for this procedure.
  Dim fs As Object
   
      'Assume it does not
      FileExists = False
      
      Set fs = CreateObject("Scripting.FileSystemObject")
      
      'FileExists returns True or False
      FileExists = fs.FileExists(FilePath)
   
  End Function
   

Folder Exists

  Function FolderExists(DirectoryPath As String) As Boolean
  'The reference for the FileSystemObject Object is Windows Script Host Object Model
  'but it not necessary to add the reference for this procedure.
  Dim fs As Object
   
      'Assume it does not
      FolderExists = False
      
      Set fs = CreateObject("Scripting.FileSystemObject")
      
      'FolderExists returns True or False
      FolderExists = fs.FolderExists(DirectoryPath)
   
  End Function
   

Drive Exists

  Function DriveExists(DirectoryPath As String) As Boolean
  'The reference for the FileSystemObject Object is Windows Script Host Object Model
  'but it not necessary to add the reference for this procedure.
  Dim fs As Object
   
      'Assume it does not
      DriveExists = False
      
      Set fs = CreateObject("Scripting.FileSystemObject")
      
      'DriveExists returns True or False
      DriveExists = fs.DriveExists(DirectoryPath)
   
  End Function
  

Further Information VBE Property: http://msdn2.microsoft.com/en-us/library/6kxy1a51(VS.85).aspx

405 Rating: 2.0/5 (3 votes cast)