Fastest way to check if file exists

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
I have 1000s of files to sort through in a directory and I was wondering if anyone has experience on which is the best way to check if a file exists. From my research the two approaches are the Dir function and the other is to createa a filesystemobject and use FileExists function. Does anyone know which is naturally faster in their search function?

Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Scratch both methods. They are insanely slow when you are looking in the 1000s. Does anyone have a faster method?
 
Upvote 0
How about
Code:
Sub bradyboyy88()
   Dim Pth As String
   Dim Fname As String
   
   Pth = "C:\MrExcel\"
   Fname = "RWC 2019.xlsx"
   Debug.Print FileExists(Pth, Fname)
End Sub

Function FileExists(Pth As String, Fname As String) As Boolean
   FileExists = Dir(Pth & Fname) <> ""
End Function
 
Upvote 0
How about
Code:
Sub bradyboyy88()
   Dim Pth As String
   Dim Fname As String
   
   Pth = "C:\MrExcel\"
   Fname = "RWC 2019.xlsx"
   Debug.Print FileExists(Pth, Fname)
End Sub

Function FileExists(Pth As String, Fname As String) As Boolean
   FileExists = Dir(Pth & Fname) <> ""
End Function

Thanks for the reply fluff but Dir is insanely slow similar to the file system object FileExist function :( . I am curious if there is any other methods.
 
Upvote 0
What are you trying to do?
 
Upvote 0
Just run a quick test & looping through 1022 filenames in Col A & putting True or false in colB took less then 0.2seconds
 
Upvote 0
Basically we have a ado recordset with a list of directories then we scroll through the recordset to see if that directory/file truly exists. The issue I am finding is these files are not local drives but instead network drives so they are a bit more time consuming to go through. I did not know of any other way to account for that then to find the quickest method possible.
 
Upvote 0
OK, I know nothing about ado recordsets as I've never used them.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top