PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Re: Classic VB - How can I check if a file exists?-VBForums
Results 1 to 15 of 15

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

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    27

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

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,098

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

    Thread moved from this thread in the FAQ forum, as the FAQ forum is not the place to post your questions.


    The answer to your question is basically yes... but it depends on the value of App.Path because if the path is the root of a drive (eg: "C:\") then App.Path gets confused with the \ , so you need to deal with that.

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

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

    Quote Originally Posted by si_the_geek View Post
    ... but it depends on the value of App.Path because if the path is the root of a drive (eg: "C:\") then App.Path gets confused with the \ , so you need to deal with that.
    Using this version of the FileExists function ...

    Code:
    Public Function FileExists(ByRef sFileName As String) As Boolean
        On Error Resume Next
        FileExists = (GetAttr(sFileName) And vbDirectory) <> vbDirectory
    End Function
    ... the following code seems to work correctly ...

    Code:
    Private Sub Main()
        If FileExists(App.Path & "\\\\\/////filename.txt") Then
            MsgBox "Existing!", vbInformation
        Else
            MsgBox "Not Existing!", vbInformation
        End If
    End Sub
    ... even when the exe and text file were placed in any drive's root directory.

    It appears that the GetFileAttributes API function sanitizes the supplied path first. I've noticed this behavior in other File System APIs too, although I can't recall whether FindFirstFile behaves the same.



    @ PSXGamerPro1

    You might also want to check out The Optimum FileExists Function.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,296

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

    IMO, the best and easiest way is to simply use the API function.

    Code:
    Public Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    
    If PathFileExists("C:\whatever.wtv") Then
    It returns 0 if the file/folder doesn't exist. There's also PathFileExistsW if you're dealing with Unicode. I use this function extensively, it works fine in VB. Just pass the full directory info.
    Last edited by fafalone; Jul 6th, 2013 at 06:57 PM.

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

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

    Quote Originally Posted by fafalone View Post
    IMO, the best and easiest way is to simply use the API function.

    Code:
    Public Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    
    If PathFileExists("C:\whatever.wtv") Then
    It returns 0 if the file/folder doesn't exist. There's also PathFileExistsW if you're dealing with Unicode. I use this function extensively, it works fine in VB. Just pass the full directory info.
    From Visual C++ in Short: Determining whether a path refers to a file system object:

    Quote Originally Posted by Kenny Kerr
    The shell provides the PathFileExists function which is simpler than the approaches mentioned thus far but is limited in that it does not distinguish between files and directories.

    . . .

    Incidentally, the PathFileExists function I mentioned above uses GetFileAttributes internally if it determines that you’re running on a supported version of Windows.
    From Superstition: Why is GetFileAttributes the way old-timers test file existence?:

    Quote Originally Posted by Raymond Chen
    If you ask an old-timer how to test for file existence, they'll say, "Use GetFileAttributes." This is still probably the quickest way to test for file existence, since it requires only a single call. Other methods such as FindFirstFile or CreateFile require a separate FindClose or CloseHandle call, which triggers another network round-trip, which adds to the cost.
    Through extensive research and experimentation, I believe I've found The Optimum FileExists Function.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,426

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

    I'm always amazed by the number of VB coders who think using API calls makes their code whizzy and 'leet.


    Lots of things built into VB like GetAttr() are very thin wrappers on API calls.

    Worse yet using the Declare Function... syntax wraps even more overhead around the API call than you have by using the built-in function! Not only does this resolve the entrypoint interpretively at runtime by scanning the DLL's export table, anything passed ByVal... As String entails two copy operations that convert from Unicode to ANSI and back, and extra GetLastError API calls get made.

    You can improve on that by creating a type library and using wide-character entrypoints, avoiding copy/convert and resolving entrypoints at compile time. But hardly anyone does that anymore.

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

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

    What are you talking about, dilettante?

    Wrappers are usually slower than direct calls to windows api. Although I may agree that using api isn't always necessary it's often much faster approach.
    You may discuss unicode/ansi conversion for as long as you want however in the end the fastest time wins. Try doing recursive search without FindFirst using perhaps Dir function which is nothing more than a wrapper...
    Regardless how thin is the wrapper it's still a wrapper.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,426

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

    When you use Declare Function your calls are anything but direct, and this imposes the performance penalties I described above. As a result it is often quite a bit slower than using native operations in VB.

    I have no idea why you think converting strings back and forth between character sets might be free. Or for that matter why you don't realize that calls to a "Declared" entrypoint are "wrapped."

    I'm not sure where you get the notion that the Dir$() function is particularly slow.

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

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

    Quote Originally Posted by dilettante View Post
    Lots of things built into VB like GetAttr() are very thin wrappers on API calls.

    Worse yet using the Declare Function... syntax wraps even more overhead around the API call than you have by using the built-in function!
    Well, according to my tests, the following FileExists function:

    Code:
    Public Function FileExists(ByRef sFileName As String) As Boolean
        On Error Resume Next
        FileExists = (GetAttr(sFileName) And vbDirectory) <> vbDirectory
    End Function
    was slower than this one:

    Code:
    Private Declare Function GetFileAttributesW Lib "kernel32.dll" (ByVal lpFileName As Long) As Long
    
    Public Function FileExists(ByRef sFileName As String) As Boolean
        Const ERROR_SHARING_VIOLATION = 32&
    
        Select Case (GetFileAttributesW(StrPtr(sFileName)) And vbDirectory) = 0&
            Case True: FileExists = True
            Case Else: FileExists = Err.LastDllError = ERROR_SHARING_VIOLATION
        End Select
    End Function
    by about one second after 100,000 iterations (with both existing and non-existent files).



    I think an unbiased benchmark should be able to settle this API vs intrinsic function debate once and for all.



    EDIT

    GetAttr probably lost due to ANSI-Unicode conversion on the OS side. As mentioned in Unicode and ANSI Functions:

    Quote Originally Posted by MSDN
    The ANSI versions are also less efficient, because the operating system must convert the ANSI strings to Unicode at run time.
    Last edited by Bonnie West; Jul 7th, 2013 at 01:47 PM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

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

    Thanks Bonnie for posting your test. Here is another great example of wrapper vs api comparison (sorry but I don't have tool to write my own right now).
    Paraphrasing what Bonnie said it's pointless to debate this matter any further.

    edit: was able to run some tests so below are the results from reading System32 folder:

    api 1 (using code posted here) - 2.9 sec
    api 2 (using code posted here) - 0.51 sec
    fso (using code posted here) - 8.1 sec
    dir wrapper (using code posted here) - a whooping 26.9 sec

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

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

    Code:
    Option Explicit
    
    Private Type WIN32_FIND_DATA    'Modified UDT
        dwFileAttributes As Long
        ftCreationTime   As Currency
        ftLastAccessTime As Currency
        ftLastWriteTime  As Currency
        nFileSize        As Currency
        dwReserved       As Currency
        cFileName(519)   As Byte
        cAlternate(27)   As Byte
    End Type
    
    Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
    Private Declare Function FindFirstFileW Lib "kernel32.dll" (ByVal lpFileName As Long, ByRef lpFindFileData As Any) As Long
    Private Declare Function QueryPerformanceCounter Lib "kernel32.dll" (ByRef lpPerformanceCount As Currency) As Long
    Private Declare Function QueryPerformanceFrequency Lib "kernel32.dll" (ByRef lpFrequency As Currency) As Long
    Private Declare Sub Sleep Lib "kernel32.dll" (Optional ByVal dwMilliseconds As Long)
    
    Private Sub Main()
        Const FILE1 = "C:\\\\\Windows\/\/\System32/////msvbvm60.dll"
        Const FILE2 = "C:\\\\\Windows\/\/\System32/////msvbvm70.dll"
        Dim RV As Boolean, i As Long, Iterations As Long, sRetVal As String
        Dim StartAPI As Currency, StopAPI As Currency, Freq As Currency
        Dim StartDIR As Currency, StopDIR As Currency
    
        MsgBox "FILE1 = """ & FILE1 & """" & vbNewLine & _
               "FileExistsAPI(FILE1) = " & FileExistsAPI(FILE1) & vbNewLine & _
               "FileExistsDIR(FILE1) = " & FileExistsDIR(FILE1) & vbNewLine & vbNewLine & _
               "FILE2 = """ & FILE2 & """" & vbNewLine & _
               "FileExistsAPI(FILE2) = " & FileExistsAPI(FILE2) & vbNewLine & _
               "FileExistsDIR(FILE2) = " & FileExistsDIR(FILE2), vbInformation, "FileExists"
    
        Do: sRetVal = InputBox("Loop how many times?", "FileExistsAPI vs FileExistsDIR", "500,000")
            If StrPtr(sRetVal) = 0& Then Exit Sub    '  Abort benchmark if canceled
            On Error Resume Next
            Iterations = CLng(sRetVal) - 1&
        Loop While Err
    
        If QueryPerformanceFrequency(Freq) Then
            Sleep
            QueryPerformanceCounter StartAPI
                For i = 0& To Iterations
                    RV = FileExistsAPI(FILE1)   'Existing
                    RV = FileExistsAPI(FILE2)   'Missing
                Next
            QueryPerformanceCounter StopAPI
    
            Sleep
            QueryPerformanceCounter StartDIR
                For i = 0& To Iterations
                    RV = FileExistsDIR(FILE1)   'Existing
                    RV = FileExistsDIR(FILE2)   'Missing
                Next
            QueryPerformanceCounter StopDIR
    
            MsgBox FormatNumber(Iterations + 1&, 0&) & " Iterations" & vbNewLine & vbNewLine & _
                   "API = " & Round((StopAPI - StartAPI) / Freq, 3&) & " secs" & vbNewLine & _
                   "DIR = " & Round((StopDIR - StartDIR) / Freq, 3&) & " secs", vbInformation, _
                   "FileExistsAPI vs FileExistsDIR"
        End If
    End Sub
    
    Public Function FileExistsAPI(ByRef sFileName As String) As Boolean
        Dim WFD As WIN32_FIND_DATA
    
        If LenB(sFileName) Then _
            If FindClose(FindFirstFileW(StrPtr(sFileName), WFD)) Then _
                FileExistsAPI = (WFD.dwFileAttributes And vbDirectory) <> vbDirectory
    End Function
    
    Public Function FileExistsDIR(ByRef sFileName As String) As Boolean
        Const FILE_ATTRIBS = vbArchive Or vbHidden Or vbReadOnly Or vbSystem
    
        If LenB(sFileName) Then FileExistsDIR = LenB(Dir(sFileName, FILE_ATTRIBS)) <> 0&
    End Function
    Code:
    Vista    500,000x
    -----------------
    API = 75.787 secs
    DIR = 88.540 secs
    
    
    Win 7    500,000x
    -----------------
    API = 86.682 secs
    DIR = 99.917 secs

    Verdict: API rules!



    Exe optimized for Fast Code with all Advanced Optimizations on. Both benchmarks performed in Safe Mode with Command Prompt. Process boosted to Realtime priority. Tested with both Vista and Win 7 on the same machine.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    27

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

    Quote Originally Posted by Bonnie West View Post
    Code:
    Option Explicit
    
    Private Type WIN32_FIND_DATA    'Modified UDT
        dwFileAttributes As Long
        ftCreationTime   As Currency
        ftLastAccessTime As Currency
        ftLastWriteTime  As Currency
        nFileSize        As Currency
        dwReserved       As Currency
        cFileName(519)   As Byte
        cAlternate(27)   As Byte
    End Type
    
    Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
    Private Declare Function FindFirstFileW Lib "kernel32.dll" (ByVal lpFileName As Long, ByRef lpFindFileData As Any) As Long
    Private Declare Function QueryPerformanceCounter Lib "kernel32.dll" (ByRef lpPerformanceCount As Currency) As Long
    Private Declare Function QueryPerformanceFrequency Lib "kernel32.dll" (ByRef lpFrequency As Currency) As Long
    Private Declare Sub Sleep Lib "kernel32.dll" (Optional ByVal dwMilliseconds As Long)
    
    Private Sub Main()
        Const FILE1 = "C:\\\\\Windows\/\/\System32/////msvbvm60.dll"
        Const FILE2 = "C:\\\\\Windows\/\/\System32/////msvbvm70.dll"
        Dim RV As Boolean, i As Long, Iterations As Long, sRetVal As String
        Dim StartAPI As Currency, StopAPI As Currency, Freq As Currency
        Dim StartDIR As Currency, StopDIR As Currency
    
        MsgBox "FILE1 = """ & FILE1 & """" & vbNewLine & _
               "FileExistsAPI(FILE1) = " & FileExistsAPI(FILE1) & vbNewLine & _
               "FileExistsDIR(FILE1) = " & FileExistsDIR(FILE1) & vbNewLine & vbNewLine & _
               "FILE2 = """ & FILE2 & """" & vbNewLine & _
               "FileExistsAPI(FILE2) = " & FileExistsAPI(FILE2) & vbNewLine & _
               "FileExistsDIR(FILE2) = " & FileExistsDIR(FILE2), vbInformation, "FileExists"
    
        Do: sRetVal = InputBox("Loop how many times?", "FileExistsAPI vs FileExistsDIR", "500,000")
            If StrPtr(sRetVal) = 0& Then Exit Sub    '  Abort benchmark if canceled
            On Error Resume Next
            Iterations = CLng(sRetVal) - 1&
        Loop While Err
    
        If QueryPerformanceFrequency(Freq) Then
            Sleep
            QueryPerformanceCounter StartAPI
                For i = 0& To Iterations
                    RV = FileExistsAPI(FILE1)   'Existing
                    RV = FileExistsAPI(FILE2)   'Missing
                Next
            QueryPerformanceCounter StopAPI
    
            Sleep
            QueryPerformanceCounter StartDIR
                For i = 0& To Iterations
                    RV = FileExistsDIR(FILE1)   'Existing
                    RV = FileExistsDIR(FILE2)   'Missing
                Next
            QueryPerformanceCounter StopDIR
    
            MsgBox FormatNumber(Iterations + 1&, 0&) & " Iterations" & vbNewLine & vbNewLine & _
                   "API = " & Round((StopAPI - StartAPI) / Freq, 3&) & " secs" & vbNewLine & _
                   "DIR = " & Round((StopDIR - StartDIR) / Freq, 3&) & " secs", vbInformation, _
                   "FileExistsAPI vs FileExistsDIR"
        End If
    End Sub
    
    Public Function FileExistsAPI(ByRef sFileName As String) As Boolean
        Dim WFD As WIN32_FIND_DATA
    
        If LenB(sFileName) Then _
            If FindClose(FindFirstFileW(StrPtr(sFileName), WFD)) Then _
                FileExistsAPI = (WFD.dwFileAttributes And vbDirectory) <> vbDirectory
    End Function
    
    Public Function FileExistsDIR(ByRef sFileName As String) As Boolean
        Const FILE_ATTRIBS = vbArchive Or vbHidden Or vbReadOnly Or vbSystem
    
        If LenB(sFileName) Then FileExistsDIR = LenB(Dir(sFileName, FILE_ATTRIBS)) <> 0&
    End Function
    Code:
    Vista    500,000x
    -----------------
    API = 75.787 secs
    DIR = 88.540 secs
    
    
    Win 7    500,000x
    -----------------
    API = 86.682 secs
    DIR = 99.917 secs

    Verdict: API rules!



    Exe optimized for Fast Code with all Advanced Optimizations on. Both benchmarks performed in Safe Mode with Command Prompt. Process boosted to Realtime priority. Tested with both Vista and Win 7 on the same machine.
    Yes! API is awesome and Quicker. I should use API for anything I might want to get my projects to do.Name:  wink.gif
Views: 576
Size:  1.0 KB
    Last edited by PSXGamerPro1; Jul 8th, 2013 at 08:11 AM.

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,895

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

    Personally I would just use the DIR$() function. A while back we did some testing on the various methods of searching for files and DIR$() was right there with the API calls in terms of speed. In our test we were looping through around 40,000 files and found that the time taken with API and DIR$() was almost identical with one being about 1 second + or - of the other on each run and the one that gave the fastest time was not always the API. We also tested with the FSO which was much much slower than the other two options.

    Of course the old faithful Dos Dir was faster than anything we tested but if you add code to call it and get the results then it can take longer than the API on a listign of files that is not very long or is not scanning bunch of folders. On really large requests the Dos method seems to be the fastest and on short requests API or DIR$() are about the same.
    Last edited by DataMiser; Jul 8th, 2013 at 09:52 AM.

  14. #14
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

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

    @ DataMiser

    In your tests, which version of the API did your team use? Is it the A (ANSI) or the W (Unicode) entrypoint? Also, on which OS did your team ran the tests? The reason I'm asking is because, ANSI-Unicode conversion may be a factor in the timing results. See my edit in post #9.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  15. #15
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,355

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

    Quote Originally Posted by DataMiser View Post
    Personally I would just use the DIR$() function. A while back we did some testing on the various methods of searching for files and DIR$() was right there with the API calls in terms of speed. In our test we were looping through around 40,000 files and found that the time taken with API and DIR$() was almost identical with one being about 1 second + or - of the other on each run and the one that gave the fastest time was not always the API. We also tested with the FSO which was much much slower than the other two options.

    Of course the old faithful Dos Dir was faster than anything we tested but if you add code to call it and get the results then it can take longer than the API on a listing of files that is not very long or is not scanning bunch of folders. On really large requests the Dos method seems to be the fastest and on short requests API or DIR$() are about the same.
    Agreed. You also have to write code that checks all possible drive letters for the file. The same file name can exist on several drives. Thus, the files(s) could be anywhere on the system. It can be done and you should count and/or list them all along with their location.
    Doctor Ed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width