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

Search file path for file

We will have a large project folder structure. I would like to run an ilogic rule that searches for an excel document named "Title Block Information.xlsx". The rule should START searching where the drawing is saved, if file is found return the path, else back up one level and search again, ect. There may be multiple excel sheets in the folder structure but the rule should always find the one saved closest to the drawing.

 

Below you will find what I had when I gave up. The issues:

It searches starting at the top of the file structure and searches down to the drawing. I need it to go the other way.

 

Sub Main
	Dim MyDrawDoc As DrawingDocument=ThisDoc.Document
	Dim mySavePath As String = GetPath(MyDrawDoc)
End Sub

Private Function GetPath(oDrawDoc As DrawingDocument) As String
Try
	Dim sFullDocumentPath As String = IO.Path.GetDirectoryName(oDrawDoc.FullDocumentName)
	Dim aPathParts() As String = Strings.Split(sFullDocumentPath, "\")
	
	For i=0 To UBound(aPathParts)
		sExcelPathx = sExcelPathx & aPathparts(i) & "\"
msgbox(sExcelPathx)
		If System.IO.File.Exists(sExcelPathx & "\Title Block Information.xlsx") Then
			sExcelPathx = sExcelPathx & "Title Block Information.xlsx\"
			bFound = True
		End If
	Next
	
	'only return path if folder $Fabrication Data was found
	If bfound = True Then
		partofpath = Left(sExcelPathx, sExcelPathx.IndexOf(".xlsx"))
		Dim sExcelPath As String = partofpath & ".xlsx"
		'MsgBox(sExcelPath)
		Return sExcelPath
	Else
		Return Nothing
End Try

End Function

 

 

I think this should find your file:

Sub Main
	Dim MyDrawDoc As DrawingDocument = ThisDoc.Document
	Dim mySavePath As String = GetPath(MyDrawDoc, "\Title Block Information.xlsx")
	MessageBox.Show(mySavePath, "Result")
End Sub

Private Function GetPath(aDoc As Document, searchName As String) As String
	Dim Result As String = "Excel File not found"
	Dim sFullDocumentPath As String = aDoc.FullFileName
	Dim SearchFolder As String = Left(sFullDocumentPath, sFullDocumentPath.LastIndexOf("\"))
CheckFolder :	
	Logger.Trace(SearchFolder)
	If System.IO.File.Exists(SearchFolder & searchName)
		Result = SearchFolder & searchName
		Return Result
	Else If SearchFolder.Count(Function(c As Char) c = "\")>0
		SearchFolder = Left(SearchFolder, SearchFolder.LastIndexOf("\"))
		GoTo CheckFolder
	End If
	Return Result
End Function

 

Let me know if you have any questions, or if this is not working as intended