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
 
Actually,

I am not sure how to use the shell dir command from that post. Is this the correct syntax? Also, since exec brings up the command window is there a way to do this with run to hide the window?

Code:
sub testdir()
Dim FilePath as String
Dim FileExist as Boolean

FileExist =CreateObject("WScript.Shell").Exec("cmd /c dir " & """" & FilePath & """" & " /b /s").StdOut.ReadAll

Msgbox FileExist
End Sub
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I'm afraid I've no idea, as I've never used it myself.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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