Searching for text within the comments for an entire directory.

Searching for text within the comments for an entire directory.

shastu
Advisor Advisor
657 Views
6 Replies
Message 1 of 7

Searching for text within the comments for an entire directory.

shastu
Advisor
Advisor

Does anyone already have something to be able to look through a directory of part files and locate all the files with certain text in the comments iProperty of the Summary tab?

0 Likes
Accepted solutions (1)
658 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Here's something you can use for that.

Imports System.Windows.Forms

Dim oSFolder As String
Dim oFDialog As New FolderBrowserDialog
oFDialog.Description = "Select directory to search within."
oFDialog.RootFolder = System.Environment.SpecialFolder.MyComputer
Dim oResult As DialogResult = oFDialog.ShowDialog()
If oResult = DialogResult.OK Then
	oSFolder = oFDialog.SelectedPath
End If
'MsgBox(oSFolder)
Dim oFileNames() As String = System.IO.Directory.GetFiles(oSFolder, "*.ipt", IO.SearchOption.TopDirectoryOnly)
Dim oFiles As New List(Of String)
Dim oPDoc As PartDocument
Dim oComments As [Property]
Dim oComment As String
For Each oFileName As String In oFileNames
	oPDoc = ThisApplication.Documents.Open(oFileName, False)
	oComments = oPDoc.PropertySets.Item("Inventor Summary Information").Item("Comments")
	oComment = oComments.Value
	If oComment.Contains("specific text") Then
		oFiles.Add(oPDoc.FullFileName)
	End If
	oPDoc.ReleaseReference
Next
ThisApplication.Documents.CloseAll(True)
a = InputListBox("Here is the list of files that contained that text within their Comments.",oFiles)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

shastu
Advisor
Advisor

Is this something I can run in VBA?  Do I need to add a reference for it to work or what am I missing?  I should have specified that I want it to be able to be run from within Inventor VBA.

0 Likes
Message 4 of 7

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @shastu 

The code @WCrihfield wrote for you is iLogic 🙂

You can try this for VBA (Cred to @Lewis.Young for the VBA folder browser)

https://forums.autodesk.com/t5/inventor-customization/how-to-pick-a-folder-in-vba/td-p/8385205

 

 

Sub FindFilesWithComment()
Dim oComment As String
oComment = "Hello" 'Comment to search for
Dim FolderPath As String
FolderPath = BrowseForFolder()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim oFiles As Object
 If FolderPath = "" Then
 Exit Sub
 End If
Dim oFileNames As String
oFileNames = "Files with comments: " & oComment
Set oFSO = CreateObject("Scripting.FileSystemObject")
 
Set oFolder = oFSO.GetFolder(FolderPath)
For Each oFile In oFolder.Files
  If LCase(oFSO.GetExtensionName(oFile.Name)) = "ipt" Then
  Dim oDoc As PartDocument
           Set oDoc = ThisApplication.Documents.Open(FolderPath & "\" & oFile.Name, False)
           Dim commentVal As String
           commentVal = oDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
           If commentVal = oComment Then
           oFileNames = oFileNames & vbCrLf & oFile.Name
           End If
           oDoc.Close
    End If
 
Next oFile
Call MsgBox(oFileNames, , "Found Files")
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    Dim ShellApp As Object
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0
    Set ShellApp = Nothing
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
        If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
        If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
        GoTo Invalid
    End Select

    Exit Function

Invalid:
    BrowseForFolder = ""
End Function

 

Message 5 of 7

JhoelForshav
Mentor
Mentor

Maybe you wanted to see if the comments contain the text, and not only find parts where the text is exactly equal to the comments:

Sub FindFilesWithComment()
Dim oComment As String
oComment = "Hello" 'Comment to search for
Dim FolderPath As String
FolderPath = BrowseForFolder()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim oFiles As Object
 If FolderPath = "" Then
 Exit Sub
 End If
Dim oFileNames As String
oFileNames = "Files with comments: " & oComment
Set oFSO = CreateObject("Scripting.FileSystemObject")
 
Set oFolder = oFSO.GetFolder(FolderPath)
For Each oFile In oFolder.Files
  If LCase(oFSO.GetExtensionName(oFile.Name)) = "ipt" Then
  Dim oDoc As PartDocument
           Set oDoc = ThisApplication.Documents.Open(FolderPath & "\" & oFile.Name, False)
           Dim commentVal As String
           commentVal = oDoc.PropertySets.Item("Inventor Summary Information").Item("Comments").Value
           If InStr(1, commentVal, oComment, vbTextCompare) > 0 Then
           oFileNames = oFileNames & vbCrLf & oFile.Name
           End If
           oDoc.Close
    End If
 
Next oFile
Call MsgBox(oFileNames, , "Found Files")
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    Dim ShellApp As Object
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0
    Set ShellApp = Nothing
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
        If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
        If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
        GoTo Invalid
    End Select

    Exit Function

Invalid:
    BrowseForFolder = ""
End Function
Message 6 of 7

WCrihfield
Mentor
Mentor

Wish I knew you needed VBA when I posted.  I would have offered it.  I went home for the day shortly after posting the iLogic code, so I didn't get back right away.

Here's another example of a folder browser dialog using VBA.  You may need to turn on a Reference to the Microsoft Office object library, for this to work.

Sub FolderDialog()
    Dim oFolder As Integer
    oFolder = Application.FileDialog(msoFileDialogFolderPicker).Show
    If oFolder <> 0 Then
        Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1), vbInformation, "SELECTED FOLDER")
    End If
End Sub

Sorry for the delay.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

shastu
Advisor
Advisor

No problem.  That was my fault.  I should have been more specific from the beginning.  Thanks.

0 Likes