In VBA, how do I get a list of all the files with a specific extension in a specific directory?
i am unable to do Application.FileSearch
, because i am using excel 2007
In VBA, how do I get a list of all the files with a specific extension in a specific directory?
i am unable to do Application.FileSearch
, because i am using excel 2007
The following code runs about 19 times faster than using FileSystemObject. On my machine, finding 4000 files in three different drectories took 1.57 seconds using FileSystemObject, but only 0.08 seconds using this code.
Sample usage:
(The "DoEvents" is not necessary, but allows you to use Pause/Break if needed.)
In response to your comment "so how many times do i know to run it?", this example runs until it lists all the files whose names match strPattern. Change the strFolder constant.
Alternative option: use the "Microsoft Scripting Runtime" library (check it in Tools...References) for the FileSystemObject family of objects. Something like the following, perhaps:
Dir("C:\yourPath\*.ESY", vbNormal) Returns the first file with esy extension. Each subsequent call to Dir() returns the next.