Grabbing file names and placing in array

BParra

New Member
Joined
Mar 31, 2002
Messages
2
I want to load an array with files names that are found in a folder. Right now I can load an array by hand and all other processes work perfect. The only thing I cant get to work is auto loading the array. I do not have a lot of skill here so any help would be greatly appreciated. Any thoughts?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,

How about this code? It basically uses the Filesearch method and then copies what it finds to an array. Depending on what you need it for this may be unnecessary duplication i.e. you may just be able to use the Filesearch object.

HTH,
Dan.

Code:
Sub LoadArray()
Dim strFileArray()
Dim lngLoop As Long

With Application.FileSearch
    .NewSearch
    .LookIn = "C:temp"
    .SearchSubFolders = True
    .FileType = msoFileTypeAllFiles
    .Execute
    
    ReDim strFileArray(.FoundFiles.Count)
    
    For lngLoop = 1 To .FoundFiles.Count
        strFileArray(lngLoop) = .FoundFiles(lngLoop)
    Next lngLoop
End With

End Sub
 
Upvote 0
Thanks for the quick response!! I will give this a try. Are there resources where I can find info on objects like the 'file search object' you listed?
 
Upvote 0
A good place to find info on objects is in the Object Browser (in the VB editor press F2). Filesearch appears in the Classes list. Select it and press F1 to open VBA help and you'll get a good description of what it is and how to use it.

Regards,
Dan
 
Upvote 0
Hi
I would spend my time learning about the File System Object. The filesearch is limited to the application, the newer Microsoft Scripting Runtime(File System Object) works across applications.
Tom
 
Upvote 0
Hi
I would spend my time learning about the File System Object. The filesearch is limited to the application, the newer Microsoft Scripting Runtime(File System Object) works across applications.
Tom

Hi Tom,

I don't get what you mean by 'works across applications'. Do you mean that the file types you can return with Filesearch is limited? The FileSystemObject is powerful and can do things that the FileSearchObject can't do - I've spent plenty of time working with it. However, to search a folder and its subfolders you have to utilise a recursive procedure (not the easiest thing for a beginner to get their head round). Also, any code which uses FSO won't run as fast as Filesearch - if you can prove me wrong (e.g. implement the code I posted using FSO which runs quicker) then I'll be impressed and I'll hush my mouth :)

Regards,
Dan
 
Upvote 0

Forum statistics

Threads
1,213,479
Messages
6,113,894
Members
448,530
Latest member
yatong2008

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