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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I'm afraid I've no idea, as I've never used it myself.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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