Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21266

Re: Classic VB - How can I check if a file exists?

$
0
0
Quote:

Originally Posted by dee-u View Post
We can also utilize the API's FindFirstFile and FindNextFile to determine the existence of a file or a folder, it can also support wildcard searches which I demonstrate here.

vb Code:
  1. 'In a standard Module
  2. Option Explicit
  3.  
  4. Public Const MAX_PATH                   As Long = 260
  5. Private Const ERROR_NO_MORE_FILES       As Long = 18&
  6. Private Const FILE_ATTRIBUTE_NORMAL     As Long = &H80
  7.  
  8. Private Type FILETIME
  9.     dwLowDateTime   As Long
  10.     dwHighDateTime  As Long
  11. End Type
  12.  
  13. Private Type WIN32_FIND_DATA
  14.     dwFileAttributes    As Long
  15.     ftCreationTime      As FILETIME
  16.     ftLastAccessTime    As FILETIME
  17.     ftLastWriteTime     As FILETIME
  18.     nFileSizeHigh       As Long
  19.     nFileSizeLow        As Long
  20.     dwReserved0         As Long
  21.     dwReserved1         As Long
  22.     cFileName           As String * MAX_PATH
  23.     cAlternate          As String * 14
  24. End Type
  25.  
  26. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" ( _
  27.                 ByVal lpFileName As String, _
  28.                 lpFindFileData As WIN32_FIND_DATA) As Long
  29. Private Declare Function FindClose Lib "kernel32" ( _
  30.                 ByVal hFindFile As Long) As Long
  31. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" ( _
  32.                 ByVal hFindFile As Long, _
  33.                 lpFindFileData As WIN32_FIND_DATA) As Long
  34.  
  35. Public Function FileExists(ByVal sFile As String) As Boolean
  36.     Dim lpFindFileData  As WIN32_FIND_DATA
  37.     Dim lFileHandle     As Long
  38.     Dim lRet            As Long
  39.     Dim sTemp           As String
  40.     Dim sFileExtension  As String
  41.     Dim sFileName       As String
  42.     Dim sFileData()     As String
  43.     Dim sFileToCompare  As String
  44.    
  45.     If IsDirectory(sFile) = True Then
  46.         sFile = AddSlash(sFile) & "*.*"
  47.     End If
  48.    
  49.     If InStr(sFile, ".") > 0 Then
  50.         sFileToCompare = GetFileTitle(sFile)
  51.         sFileData = Split(sFileToCompare, ".")
  52.         sFileName = sFileData(0)
  53.         sFileExtension = sFileData(1)
  54.     Else
  55.         Exit Function
  56.     End If
  57.    
  58.     ' get a file handle
  59.     lFileHandle = FindFirstFile(sFile, lpFindFileData)
  60.     If lFileHandle <> -1 Then
  61.         If sFileName = "*" Or sFileExtension = "*" Then
  62.             FileExists = True
  63.         Else
  64.             Do Until lRet = ERROR_NO_MORE_FILES
  65.                 ' if it is a file
  66.                 If (lpFindFileData.dwFileAttributes And FILE_ATTRIBUTE_NORMAL) = vbNormal Then
  67.                     sTemp = StrConv(RemoveNull(lpFindFileData.cFileName), vbProperCase)
  68.                    
  69.                     'remove LCase$ if you want the search to be case sensitive
  70.                     If LCase$(sTemp) = LCase$(sFileToCompare) Then
  71.                         FileExists = True ' file found
  72.                         Exit Do
  73.                     End If
  74.                 End If
  75.                 'based on the file handle iterate through all files and dirs
  76.                 lRet = FindNextFile(lFileHandle, lpFindFileData)
  77.                 If lRet = 0 Then Exit Do
  78.             Loop
  79.         End If
  80.     End If
  81.    
  82.     ' close the file handle
  83.     lRet = FindClose(lFileHandle)
  84. End Function
  85.  
  86. Private Function IsDirectory(ByVal sFile As String) As Boolean
  87.     On Error Resume Next
  88.     IsDirectory = ((GetAttr(sFile) And vbDirectory) = vbDirectory)
  89. End Function
  90.  
  91. Private Function RemoveNull(ByVal strString As String) As String
  92.     Dim intZeroPos As Integer
  93.  
  94.     intZeroPos = InStr(strString, Chr$(0))
  95.     If intZeroPos > 0 Then
  96.         RemoveNull = Left$(strString, intZeroPos - 1)
  97.     Else
  98.         RemoveNull = strString
  99.     End If
  100. End Function
  101.  
  102. Public Function GetFileTitle(ByVal sFileName As String) As String
  103.     GetFileTitle = Right$(sFileName, Len(sFileName) - InStrRev(sFileName, "\"))
  104. End Function
  105.  
  106. Public Function AddSlash(ByVal strDirectory As String) As String
  107.     If InStrRev(strDirectory, "\") <> Len(strDirectory) Then
  108.         strDirectory = strDirectory + "\"
  109.     End If
  110.     AddSlash = strDirectory
  111. End Function

Sample Usage:
VB Code:
  1. 'specific file
  2. If FileExists("C:\WINDOWS\system32\progman.exe") Then
  3.     MsgBox "Existing!"
  4. Else
  5.     MsgBox "Not Existing!"
  6. End If
  7.  
  8. 'check existence of folder
  9. If FileExists("C:\Program Files") Then
  10.     MsgBox "Existing!"
  11. Else
  12.     MsgBox "Not Existing!"
  13. End If
  14.  
  15. 'wildcard search1
  16. If FileExists("C:\WINDOWS\system32\pschdprf.*") Then
  17.     MsgBox "Existing!"
  18. Else
  19.     MsgBox "Not Existing!"
  20. End If
  21.  
  22. 'wildcard search2
  23. If FileExists("C:\WINDOWS\system32\*.dll") Then
  24.     MsgBox "Existing!"
  25. Else
  26.     MsgBox "Not Existing!"
  27. End If
  28.  
  29. 'wildcard search3
  30. If FileExists("C:\WINDOWS\*.*") Then
  31.     MsgBox "Existing!"
  32. Else
  33.     MsgBox "Not Existing!"
  34. End If

Will it work if you put.
If FileExists(App.Path & "\filename.txt") Then
MsgBox "Existing!"
Else
MsgBox "Not Existing!"
End If

Viewing all articles
Browse latest Browse all 21266

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>