September 5, 1995
The MS-DOS® PATH statement tells the operating system to look for files in specific directories on your disk. This article explains how to find out whether a specific file exists in one of the PATH directories.
When you write a program in Microsoft® Visual Basic®, you may need to determine if a specific file exists on a disk drive. At installation time, many software packages modify the MS-DOS® PATH statement in the AUOTOEXEC.BAT file by adding a new directory to the existing PATH directories. This allows an application to find its own system or data files that it requires to run successfully. You can add a search routine to your program to search these directories for an individual file.
The first step is to retrieve the entire path for the specified disk drive. The Visual Basic CurDir$ function returns the current disk drive's path.
Next, you need to call two Microsoft Windows® application programming interface (API) functions, GetWindowsDirectory and GetSystemDirectory. The GetWindowsDirectory function retrieves the path of the Windows directory. Windows stores its initialization files, help files, application files, and other files in this directory. The GetSystemDiectory function retrieves the path of the Windows system directory. Windows stores library, font, drive, and other system files in this directory.
In the example program below, you use all three functions mentioned above to build a string (PathStr) that contains the directory names. The IsFileInPath function simply uses the InStr function to extract each individual directory name from PathStr. Then you use the Dir$ function to determine whether the target file exists in that directory.
This program shows how to determine whether a specific file exists in one of the directories in the PATH statement.
Private Declare Function GetSystemDirectory Lib "kernel32" Alias
"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long)
As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias
"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long)
As Long
Private Sub Command1_Click()
Dim DirStr As String
Dim FileToFind As String
Dim Flag As Integer
FileToFind = Text1.Text
Flag = IsFileInPath(FileToFind, DirStr)
If Flag Then
MsgBox "File Exists in: " & DirStr
Else
MsgBox "File does not exist in PATH"
End If
End Sub
Sub BuildSearchPath(PathStr As String)
Dim RetVal As Integer
Dim Buffer As String * 128
PathStr = CurDir$
RetVal = GetWindowsDirectory(Buffer, 128)
PathStr = PathStr & ";" & Mid$(Buffer, 1, RetVal)
Buffer = Space(128)
RetVal = GetSystemDirectory(Buffer, 128)
PathStr = PathStr & ";" & Mid$(Buffer, 1, RetVal)
PathStr = PathStr & ";" & App.Path
PathStr = PathStr & ";" & Environ$("PATH")
End Sub
Function IsFileInPath(TheFile As String, DirName As String) As Integer
Dim Separator As Integer
Dim SearchStr As String
Dim Results As String
Call BuildSearchPath(SearchStr)
While Len(SearchStr) <> 0
Separator = InStr(SearchStr, ";")
If Separator <> 0 Then
DirName = Mid$(SearchStr, 1, Separator - 1)
SearchStr = Mid$(SearchStr, Separator + 1)
Else
DirName = SearchStr
SearchStr = ""
End If
Results = Dir$(DirName & "\" & TheFile)
If Results <> "" Then
IsFileInPath = True
Exit Function
End If
Wend
IsFileInPath = False
End Function
Run the example program by pressing f5. Type the name of a file that you want to find in the Text Box control. Click the command button. A message box will be displayed, telling you whether the file was found in one of the PATH directories.
"GetSystemDirectory." (MSDN Library, SDK Documentation, Platform SDK)
"GetWindowsDirectory." (MSDN Library, SDK Documentation, Platform SDK)