Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Search Header Directory and all Sub Directories for a File.

C_Haines_ENG
Collaborator

Search Header Directory and all Sub Directories for a File.

C_Haines_ENG
Collaborator
Collaborator

I am looking to create a search program that will go through a rather large folder heiarchy and find a specific file. This way I can print, open, copy, etc without having to remember exactly where a file is stored.

 

Ive seen code HERE that searches through a single directory, but I was hoping there would be a more efficient way of doing this without "For" looping every sub directory.

0 Likes
Reply
Accepted solutions (2)
344 Views
4 Replies
Replies (4)

dalton98
Collaborator
Collaborator
Accepted solution

you need to change the search option. ex:

Dim fi As System.IO.FileInfo() = FileLocation.GetFiles("*" & partOfName & "*", IO.SearchOption.AllDirectories)
0 Likes

Dev_rim
Advocate
Advocate
Accepted solution

Hi there,

You can search for some file in all sub directories too:

 

Dim filePaths As String() = Directory.GetFiles(YOUR_HEADER_FOLDER_PATH, YOUR_FILTER, SearchOption.AllDirectories)

 

 

The importing thing is the use System.IO.Directory class and add the third parameter SearchOption.AllDirectories

YOUR_HEADER_FOLDER_PATH should be your header folders path. Lets say I want to get every file on public desktop. Then its = "C:\Users\Public\Desktop"

And I want to get all the files contain "TEST" on their name. Then Filter will be "*TEST*".

so at the end this is the final:

 

Dim filePaths As String() = Directory.GetFiles("C:\Users\Public\Desktop", "*TEST*", SearchOption.AllDirectories)

 

 

For example if you want to get all Part Document files in C disk, you can use:

 

Dim filePaths As String() = Directory.GetFiles("C:\", "*.ipt", SearchOption.AllDirectories)

 

You can use Wildcards on filter. Further information about the wildcards:

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/wildcard-charact...

 

I hope it helps.

If my answer is solved your problem, please mark it as Solution

Freundliche Grüße / Kind Regards
0 Likes

C_Haines_ENG
Collaborator
Collaborator

Thank you!

0 Likes

WCrihfield
Mentor
Mentor

Hi @C_Haines_ENG.  I don't know how you are planning on using this method for (in simple iLogic rule, VBA macro, Add-In, EXE, or other), but keep in mind that the GetFiles method and EnumerateFiles methods are similar, but act differently, and it may pay off to know the difference, and use the one that is most appropriate to your situation.  When using the GetFiles method, it must completely build out its entire contents before allowing you to start iterating through its contents, which can be unnecessarily slower in certain situations, but be the appropriate one to use in other situations.  When using the EnumerateFiles method, it will allow the iteration of its contents immediately, before it has finished building the list out, which can allow the process to finish sooner in some situations.  Both have their pros & cons, and you can read more about them online if needed.  Just putting that stuff out there as food for thought.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)