Results 1 to 9 of 9

Thread: file exists

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    72
    I actually had this post in the General forum thinking more people were gonna see it, however, no response.

    Oh wells, here it goes again...

    Is the FileExists method of the FileSystemObject the only way to check if a file exists? I heard that using the FileSystemObject uses a lot of system resources. Any feedback would be greatly appreciated. Thanks.

    ttlai

  2. #2
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    I would think that using the fileexists method is the only way, if not then any other way is going to be even slower.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    You can use the Dir function.
    Used only on its own it is faster than the filesystemobject because the filesystemobject must be created first.

    But if you create the filesystemobject once, and then use the fileexists method in a loop it is faster.

    In the speed comparison below I am creating the filesystemobject everytime I need it.

    So if you only want to do a few files, use Dir(), but if you want to do loads of files, create the filesystemobject, and then use its fileexists method in a loop.

    Code:
    Option Explicit
    
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Dim timeMethod1 As Long
    Dim timeMethod2 As Long
    Dim i As Long
    Dim fs As Object
    
    Private Sub Form_Load()
        
        timeMethod1 = GetTickCount()
        For i = 0 To 10000
            Set fs = CreateObject("Scripting.FileSystemObject")
            If (fs.FileExists("c:\command.com")) Then
                
            End If
        Next i
        timeMethod1 = GetTickCount() - timeMethod1
        
        timeMethod2 = GetTickCount()
        For i = 0 To 10000
            If (Dir("c:\command.com") <> "") Then
                
            End If
        Next i
        timeMethod2 = GetTickCount() - timeMethod2
        
        
        MsgBox "Times Taken : " & vbCrLf & vbCrLf & "FileSystemObject : " & timeMethod1 & vbCrLf & "Dir Function : " & timeMethod2
        
        
    End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    Dim Fileexists  as boolean
    If len(dir$("C:\Path\File.exe")) <> 0 then Fileexists = true
    The above looks at the length of the file. Using the Dir function, but as Jamie's put, if there are a lot of files to check, use the FileSystemObject.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    doo bee doo bee doo im not being followed at all ... doo bee doo ....
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Just that the LEN(DIR$( option's faster than the Dir( option ...

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    If the file exists it is
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    72
    thanks plenderj

    i only need to check one file so i'm gonna go with the Dir() function

    thanks again

    ttlai

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I won. I won
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

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



Click Here to Expand Forum to Full Width