Search Windows Directory for a file and pull path/filename

Search Windows Directory for a file and pull path/filename

AMN3161
Advocate Advocate
1,419 Views
7 Replies
Message 1 of 8

Search Windows Directory for a file and pull path/filename

AMN3161
Advocate
Advocate

Hello everyone,

 

I just asked a question in here literally a few mins but i hit another road block. I found a solution to this but i don't want to download third party software to do it.

 

I want ilogic go search a file directory for a specific number and once it finds a file then pull that file path info a separate custom iproperty. Lets say the job number is 1130219, well i want ilogic to search a file directory including sub-folder to find that file with that name, then return the file path to said part so i can use it later in the code.

 

thank you in advanced!   

0 Likes
Accepted solutions (1)
1,420 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Try something like this:

You will need to either change the value of oSearchPath within the code, then use that first instance line, or comment that first line out, then uncomment the next line, then customize the default value for the Path to search for within that InputBox.

 

Sub Main
	Dim oSearchPath As String = "C:\Temp\"
'	Dim oStartPath As String = InputBox("Enter the directory path to start searching within.","SEARCH DIRECTORY", "C:\Temp\")
	Dim oSearchFor = InputBox("Enter a string to search for within the file names of the chosen directory.","SEARCH TERM","1130219*")
	Dim oFileNames() As String = System.IO.Directory.GetFiles(oSearchPath, oSearchFor, System.IO.SearchOption.AllDirectories)
	'Check if multiple files were found.
	If oFileNames.Length = 0 Then
		MsgBox("No files were found matching your search criteria. Exiting.", vbOKOnly, "NOTHING FOUND")
		Return
	ElseIf oFileNames.Length > 1 Then
		MsgBox("There were " & oFileNames.Length & " files found matching your search criteria. Exiting.", vbOKOnly, "MULTIPLE FOUND")
		Return
	ElseIf oFileNames.Length = 1 Then
		MsgBox("One file was found matching your search criteria." & vbCrLf & _
		"Its Path is as follows:" & vbCrLf & _
		oFileNames(0), vbOKOnly, "1 MATCH FOUND")
	End If
	Dim oFilePath As String = oFileNames(0)
End Sub

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

WCrihfield
Mentor
Mentor

By the way, that search term is searching file names, and special search characters that you would normally use when searching for file names can be used.  Such as the wildcard character "*".

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8

AMN3161
Advocate
Advocate

This seems to work except for the fact i asked to search a massive directory and it locked up my Inventor, i might have narrow down the searching area

 

this works though, thank you!

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

If you want to just search the one specified directory, without searching through its sub-directories, you can change:

System.IO.Directory.GetFiles(oSearchPath, oSearchFor, System.IO.SearchOption.AllDirectories)

To:

System.IO.Directory.GetFiles(oSearchPath, oSearchFor, System.IO.SearchOption.TopDirectoryOnly)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

iLimbani
Contributor
Contributor

Hi @WCrihfield,
How can I avoid to search into folder name : OldVersions at any step.
what I mean is my search should not go through That named folder.

Thanks

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Hi @iLimbani.  Since there are only two options available for that final input (the two shown above), you would have to use the TopDirectoryOnly option.  But then iterate through each individual directory separately, that way you can check the directory name, before using the GetFiles method on it.  To get the sub directories of your main directory, you can use the very similar System.IO.Directory.GetDirectories method.  It has several variations, as you can see at the following link:

https://learn.microsoft.com/en-us/dotnet/api/system.io.directory?view=net-8.0 

As with the GetFiles method, this GetDirectories method also returns an Array of Strings.  Each String within the Array should represent the 'path' of another sub directory (usually always without the "\" at the very end).  Once you get that, you can iterate those, checking the name of the final directory in each path, to avoid the one for OldVersions.  And you can use the following tool to get that final directory name in each path:

https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getdirectoryname?view=net-8.0 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

iLimbani
Contributor
Contributor

Thank you. It helps me.

0 Likes