Visual Basic File API Routines
PathFileExists: A Local/Network File/Folder/Drive Exists Routine
     
Posted:   Saturday April 07, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   Windows XP
Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later)
Windows 98 (or Windows 95 with Internet Explorer 4.0 or later)
Author:   VBnet - Randy Birch
     

Related:  

PathMatchSpec: Searches a String Using MS-DOS Wild-Card Match
PathFindExtension: File Extension from Local/Remote/IP/UNC Filename

PathFindFileName: File Path from Local/Remote/IP/UNC Filename
Shell Path Routines: GetPathPart
PathFindNextComponent: Parse File Path Components
PathFindNextComponent: Parse String to Obtain Path Components
PathRemoveExtension: Returns Filename with Extension Removed
PathStripPath: Removes Path Portion of Fully-qualified Path/Filename

PathUnquoteSpaces: Removes Wrapping Quotes from Paths/Filenames
PathStripToRoot: Remove All Parts of a Path Except for Root Information
PathSkipRoot: Parses Path Ignoring Drive or UNC Path
PathStripToRoot: Determine if a Path Contains a Valid Drive Syntax
PathIsSameRoot: Determine if Paths Share Common Root

PathQuoteSpaces: Returns Quoted Path String if Spaces Found
     
 Prerequisites
Version 4.71 and later of Shlwapi.dll.

Windows Shlwapi.dll exposes a  great number of path-oriented functions designed for use by the Windows shell. This series of pages shows how to call these APIs, as well as the variety of results that each may return when passed one of several different file and path combinations. Each demo wraps the API into a callable function, which either return True, False or a string, as appropriate. This page shows how to call the PathFileExists API. The target must exist for this function.

Not all of the Shlwapi.dll path APIs are appropriate to all types of file and path data that can be passed.  The illustrations for each method shows the typical return results when passed a variety of strings that represent:

  • a filename alone
  • drive, file, folder and file as absolute paths
  • drive, file, folder and file as relative paths
  • drive, file, folder and file as mapped drives
  • drive, file, folder and file as UNC paths
  • with paths containing an IP address
  • as a URL

The path functions themselves can be broken down into five basic categories (the names listed below are my wrapper function names on subsequent pages, and represent only a few of the 46 pages of path functions):

  • 'Is" routines, which determine specific attributes of the passed string, i.e. IsDirectoryEmpty, IsPathARoot, IsPathAFolder, IsPathRelative, IsUNCPathValid, etc.
  • String routines, which parse or test specific parts of the path, such as HasValidDrive, GetExtensionPart, FileExists, ChangeFileExtension etc.
  • Display routines, such as PathRemoveDecoration, GetCompactedPath, ChangePathCase.
  • Validity routines ... IsPathChrValid, SetPathBackslash
  • Helper routines ... ChangePathToEnvironment, PathAppend, PathCombine, GetArgsFromPath, GetDriveFromDriveNumber

In order to show a complete demo using a combination of path and file strings representing typical application needs, each demo uses the same 26 strings as its test bed. This provides the opportunity to see exactly how each category of path information is handled by a specific call, as well as provide a single test bed upon which you can reuse the form while building a BAS module that contains just the functions you need.

To create the environment to test these routines, I created a folder on C:\ called 'coolfocus', which contained a subfolder named javatools. In the javatools folder I placed a file named 'somefile.txt'. I also placed a copy of the somefile.txt in the c:\ root folder. I shared the coolfocus folder, and mapped a drive to it (drive m).

A few of the path APIs (like the one detailed on this page) deal with actual paths, so requires that the file or path passed to the routine actually exist on the drive. But since the majority of the path functions deal with comparison and path syntax, they return the proper values whether the paths actually exist or not. This will become clearer in some of the other path demos.

The form code below creates and labels the controls appropriate for this demo by just placing a single text and label on the form, setting both to create a control array. And although each of the path functions generally target a specific type of path statement (ie target URLs, or shares, etc), each demo reuses the same 26 file/path strings.

 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function PathFileExists Lib "shlwapi" _
   Alias "PathFileExistsA" _
  (ByVal pszPath As String) As Long
  
  
Public Function FileExists(ByVal sPath As String) As Boolean

  'Determines if a file exists. This function
  'tests the validity of the file and path. It
  'works only on the local file system or on a
  'remote drive that has been mounted to a drive
  'letter.
  '
  'It will return False for remote file paths
  'that begin with the UNC names \\server
  'or \\server\share. It will also return False
  'if a mounted remote drive is out of service.
  '
  'Requires Version 4.71 and later of Shlwapi.dll
  
   FileExists = PathFileExists(sPath) = 1
   
End Function
 Form Code
To the form, add the following code:

Option Explicit

Private Sub Form_Load()

   Dim cnt As Long
   
   For cnt = 0 To 25
   
      If cnt > 0 Then
         Load Label1(cnt)
         Load Text1(cnt)
      End If
      
      With Text1(cnt)
         .Left = 300
         .Top = 210 + (300 * cnt)
         .Width = 700
         .Text = "text1 " & CStr(cnt)
         .Visible = True
      End With
      
      With Label1(cnt)
         .Left = 1200
         .Top = 250 + (300 * cnt)
         .Caption = "label1 " & CStr(cnt)
         .Visible = True
      End With
      
   Next
   
   With Command1
      .Left = 300
      .Top = Text1(25).Top + (Text1(25).Height * 1.5)
      .Caption = "FileExists"
   End With
         
  'display the strings that will be used in the call
  'file only
   Label1(0).Caption = "somefile.txt"
  
  'absolute paths
   Label1(1).Caption = "c:\"
   Label1(2).Caption = "c:\somefile.txt"
   Label1(3).Caption = "c:\javatools\"
   Label1(4).Caption = "c:\javatools\somefile.txt"
   
  'relative paths
   Label1(5).Caption = "..\..\"
   Label1(6).Caption = "..\..\somefile.txt"
   Label1(7).Caption = "..\..\javatools\"
   Label1(8).Caption = "..\..\javatools\somefile.txt"
   
  'mapped drive
   Label1(9).Caption = "m:\"
   Label1(10).Caption = "m:\somefile.txt"
   Label1(11).Caption = "m:\coolfocus\"
   Label1(12).Caption = "m:\coolfocus\somefile.txt"
   
  'UNC
   Label1(13).Caption = "\\workstation"
   Label1(14).Caption = "\\workstation\somefile.txt"
   Label1(15).Caption = "\\workstation\javatools"
   Label1(16).Caption = "\\workstation\javatools\somefile.txt"
   
  'IP address
   Label1(17).Caption = "\\192.168.1.100"
   Label1(18).Caption = "\\192.168.1.100\somefile.txt"
   Label1(19).Caption = "\\192.168.1.100\d$"
   Label1(20).Caption = "\\192.168.1.100\d$\javatools\"
   Label1(21).Caption = "\\192.168.1.100\d$\javatools\somefile.txt"
   
  'URL
   Label1(22).Caption = "ms.htm"
   Label1(23).Caption = "www.microsoft.com"
   Label1(24).Caption = "http://www.microsoft.com"
   Label1(25).Caption = "http://www.microsoft.com/ms.htm"
   
End Sub


Private Sub Command1_Click()

  'Parses a path to determine if it
  'is a directory root. The path does
  'not have to exist.
   
  'as filename alone
   Text1(0).Text = FileExists("somefile.txt")
  
  'via absolute path
   Text1(1).Text = FileExists("c:\")
   Text1(2).Text = FileExists("c:\somefile.txt")
   Text1(3).Text = FileExists("c:\javatools\")
   Text1(4).Text = FileExists("c:\javatools\somefile.txt")
  
  'via relative path
   Text1(5).Text = FileExists("..\..\")
   Text1(6).Text = FileExists("..\..\somefile.txt")
   Text1(7).Text = FileExists("..\..\javatools\")
   Text1(8).Text = FileExists("..\..\javatools\somefile.txt")
   
  'via mapped drive
   Text1(9).Text = FileExists("m:\")
   Text1(10).Text = FileExists("m:\somefile.txt")
   Text1(11).Text = FileExists("m:\coolfocus\")
   Text1(12).Text = FileExists("m:\coolfocus\somefile.txt")
  
  'via UNC
   Text1(13).Text = FileExists("\\workstation")
   Text1(14).Text = FileExists("\\workstation\somefile.txt")
   Text1(15).Text = FileExists("\\workstation\javatools")
   Text1(16).Text = FileExists("\\workstation\javatools\somefile.txt")
   
  'via IP address
   Text1(17).Text = FileExists("\\192.168.1.100")
   Text1(18).Text = FileExists("\\192.168.1.100\somefile.txt")
   Text1(19).Text = FileExists("\\192.168.1.100\d$")
   Text1(20).Text = FileExists("\\192.168.1.100\d$\javatools")
   Text1(21).Text = FileExists("\\192.168.1.100\d$\javatools\somefile.txt")
   
   'via URL
   Text1(22).Text = FileExists("ms.htm")
   Text1(23).Text = FileExists("www.microsoft.com")
   Text1(24).Text = FileExists("http://www.microsoft.com")
   Text1(25).Text = FileExists("http://www.microsoft.com/ms.htm")

End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter