18 Replies Latest reply on Aug 21, 2015 2:13 PM by Peter Persson

    Batch generated description name from the file name

    Peter Persson

      Hi,

       

      I have a few hundred parts and assembly files that does not have any description in the custom property tab. But they have a file name that could be used as a description. Are there any macros out there that can batch update all my files with a description name based on the file name?

        • Re:  Batch generated description name from the file name
          Deepak Gupta

          You could use task scheduler to add the required property with desired value. NO macro would be required.

            • Re:  Batch generated description name from the file name
              Peter Persson

              Oh, thanks, but how do i do then? I have never useed the task scheduler before.

                • Re:  Batch generated description name from the file name
                  Deepak Gupta

                  Type Task Scheduler on the start > run and then click on the update custom properties on the task scheduler. Add file(s) or folder(s), add property details and then let it process the files.

                    • Re:  Batch generated description name from the file name
                      Peter Persson

                      It seemed quite straight forward.... But not.... Adding Description - Text - $PRP:"SW-File Name" on line 1 , pressing Finish and it runs through the files, BUT i get FAIL as status on each file. Going back to the task's settings to check and can see that line 1 on the Custom Properties tab is empty . I have tried many time with the same results. Why is the line 1 deleted?????

                        • Re:  Batch generated description name from the file name
                          Peter Persson

                          Tried Description - Text - Hi

                          and that worked..

                           

                          Seems as it does not like the $PRP:"SW-File Name"

                            • Re:  Batch generated description name from the file name
                              Peter Persson

                              I can see that others has the same problem and where you recomend to run a Macro instead in this thread.

                               

                              I am trying to use task scheduler to run $PRP:"SW-File Name" to a custom property, but it will not work.  The files open and save, but no properties change.

                               

                              I will try your Macro instead.

                                • Re:  Batch generated description name from the file name
                                  Peter Persson

                                  I got your macro to work, sort of, but now the Description in the custom properties pane tab says $PRP:"SW-File Name". How to solve?

                                    • Re:  Batch generated description name from the file name
                                      Leon Wurr

                                      Try adding this to your code:

                                       

                                              ' Change Property name here

                                              Dim strModelName As String

                                              strModelName = swModel.GetPathName

                                              swModel.Extension.CustomPropertyManager(Empty).Set "Description", Right$(Left$(strModelName, Len(strModelName) - 7), Len(strModelName) - 7 - InStrRev(Left$(strModelName, Len(strModelName) - 7), "\"))

                                              'swModel.CustomInfo("OrderNumber") = "*" & "$PRP:" + Chr(34) + "SW-File Name" + Chr(34)

                                                        

                                              swModel.ForceRebuild3 (False)

                                        • Re:  Batch generated description name from the file name
                                          Peter Persson

                                          First i thought it not working if the property name is not already present in the files, as many of my files did not get any "Descriptions" even though the files had been open and saved by the macro. I can see that on the date. But with smaller test runs it seems to work well on empty properties as well...

                                           

                                          EDIT: Some files are not receptive for this macro treatment. See attached part

                                           

                                          Could you make it so it works on assembles as well.

                                            • Re:  Batch generated description name from the file name
                                              Leon Wurr

                                              The code assumed that a property "Description" already existed on your parts.

                                              Now, if your part doesn't already have that property, it creates it.

                                              It is also working on assemblies as far as I've tested it, try it out:

                                               

                                              Sub main()
                                              
                                              
                                                  Dim swApp           As SldWorks.SldWorks
                                                  Dim swModel         As SldWorks.ModelDoc2
                                                  Dim sFileName       As String
                                                  Dim nFileName       As String
                                                  Dim Path            As String
                                                  Dim nPath           As String
                                                  Dim nErrors         As Long
                                                  Dim nWarnings       As Long
                                              
                                              
                                                  Set swApp = Application.SldWorks
                                                  Path = BrowseFolder("Select A Folder")
                                                  Path = Path + "\"
                                                  'sFileName = Dir(Path & "*.sldprt")
                                                  sFileName = Dir(Path)
                                                  
                                                  Do Until sFileName = ""
                                                       'Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)
                                                       Set swModel = swApp.OpenDoc(Path + sFileName, 1)
                                                       Set swModel = swApp.OpenDoc(Path + sFileName, 2)
                                                       Set swModel = swApp.ActiveDoc
                                                       Dim strModelName As String
                                                       strModelName = swModel.GetPathName
                                                       Const PROP_VAL_1 As String = Empty
                                                       Dim swCustPropMgr As SldWorks.CustomPropertyManager
                                                       Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
                                                       swCustPropMgr.Add3 "Description", swCustomInfoText, PROP_VAL_1, swCustomPropertyOnlyIfNew
                                                       swModel.Extension.CustomPropertyManager(Empty).Set "Description", Right$(Left$(strModelName, Len(strModelName) - 7), Len(strModelName) - 7 - InStrRev(Left$(strModelName, Len(strModelName) - 7), "\"))
                                                       swModel.ForceRebuild3 (False)
                                                       swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings
                                                       swApp.CloseDoc swModel.GetTitle
                                                       Set swModel = Nothing
                                                       sFileName = Dir
                                                  Loop
                                              
                                              
                                              End Sub
                                              
                                                • Re:  Batch generated description name from the file name
                                                  Peter Persson

                                                  Thank you very much Leon.

                                                   

                                                  I got it to work with a few files, but then i got an error

                                                   

                                                   

                                                  And the debugger highlighted "strModelName = swModel.GetPathName"

                                                    • Re:  Batch generated description name from the file name
                                                      Deepak Gupta

                                                      Can you try using it separately for part and assembly as it might have got confused when there are same name for part and assembly.

                                                      • Re:  Batch generated description name from the file name
                                                        Leon Wurr

                                                        Maybe there were some drawing docs in the same folder as the parts and assemblies?

                                                         

                                                        Try this update:

                                                         

                                                        Sub main()
                                                        
                                                        
                                                            Dim swApp           As SldWorks.SldWorks
                                                            Dim swModel         As SldWorks.ModelDoc2
                                                            Dim sFileName       As String
                                                            Dim nFileName       As String
                                                            Dim Path            As String
                                                            Dim nPath           As String
                                                            Dim nErrors         As Long
                                                            Dim nWarnings       As Long
                                                        
                                                        
                                                            Set swApp = Application.SldWorks
                                                            Path = BrowseFolder("Select A Folder")
                                                            Path = Path + "\"
                                                            sFileName = Dir(Path)
                                                            
                                                            Do Until sFileName = ""
                                                                If InStr(1, UCase(sFileName), "SLDDRW") = 0 Then
                                                                    If InStr(1, UCase(sFileName), "SLDPRT") <> 0 Then
                                                                        Set swModel = swApp.OpenDoc(Path + sFileName, swDocPART)
                                                                    ElseIf InStr(1, UCase(sFileName), "SLDASM") <> 0 Then
                                                                        Set swModel = swApp.OpenDoc(Path + sFileName, swDocASSEMBLY)
                                                                    End If
                                                                    Set swModel = swApp.ActiveDoc
                                                                    Dim strModelName As String
                                                                    strModelName = swModel.GetPathName
                                                                    Const PROP_VAL_1 As String = Empty
                                                                    Dim swCustPropMgr As SldWorks.CustomPropertyManager
                                                                    Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
                                                                    swCustPropMgr.Add3 "Description", swCustomInfoText, PROP_VAL_1, swCustomPropertyOnlyIfNew
                                                                    swModel.Extension.CustomPropertyManager(Empty).Set "Description", Right$(Left$(strModelName, Len(strModelName) - 7), Len(strModelName) - 7 - InStrRev(Left$(strModelName, Len(strModelName) - 7), "\"))
                                                                    swModel.ForceRebuild3 (False)
                                                                    swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings
                                                                    swApp.CloseDoc swModel.GetTitle
                                                                    Set swModel = Nothing
                                                                End If
                                                                sFileName = Dir
                                                            Loop
                                                        
                                                        
                                                        End Sub
                                                        
                                                          • Re:  Batch generated description name from the file name
                                                            Peter Persson

                                                            I got the same problem with the new code

                                                             

                                                            strModelName = swModel.GetPathName

                                                             

                                                            I tried to move files around... and i found the problem. It does not like to have an excel file in the folder.. without the excel file, it works fine.

                                                             

                                                            Would it be possible to make so it does not overwrite any excisting names = Ignore if there is a description value in the properties already...

                                                              • Re:  Batch generated description name from the file name
                                                                Deepak Gupta

                                                                For that use

                                                                 

                                                                Sub main()  

                                                                    Dim swApp           As SldWorks.SldWorks 

                                                                    Dim swModel         As SldWorks.ModelDoc2 

                                                                    Dim sFileName       As String 

                                                                    Dim nFileName       As String 

                                                                    Dim Path            As String 

                                                                    Dim nPath           As String 

                                                                    Dim nErrors         As Long 

                                                                    Dim nWarnings       As Long 

                                                                    Set swApp = Application.SldWorks 

                                                                    Path = BrowseFolder("Select A Folder") 

                                                                    Path = Path + "\" 

                                                                    sFileName = Dir(Path) 

                                                                    Do Until sFileName = "" 

                                                                        If InStr(1, UCase(sFileName), "SLDDRW") = 0 Then 

                                                                            If InStr(1, UCase(sFileName), "SLDPRT") <> 0 Then 

                                                                                Set swModel = swApp.OpenDoc(Path + sFileName, swDocPART) 

                                                                            ElseIf InStr(1, UCase(sFileName), "SLDASM") <> 0 Then 

                                                                                Set swModel = swApp.OpenDoc(Path + sFileName, swDocASSEMBLY) 

                                                                            End If 

                                                                            Set swModel = swApp.ActiveDoc 

                                                                            Dim strModelName As String 

                                                                            strModelName = swModel.GetPathName 

                                                                            Const PROP_VAL_1 As String = Empty 

                                                                            Dim swCustPropMgr As SldWorks.CustomPropertyManager 

                                                                            Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty) 

                                                                  Dim val As String

                                                                         Dim valout As String

                                                                         swCustPropMgr.Get4 "Description", False, val, valout

                                                                         If valout = "" Then

                                                                         swCustPropMgr.Add3 "Description", swCustomInfoText, PROP_VAL_1, swCustomPropertyOnlyIfNew

                                                                         swModel.Extension.CustomPropertyManager(Empty).Set "Description", Right$(Left$(strModelName, Len(strModelName) - 7), Len(strModelName) - 7 - InStrRev(Left$(strModelName, Len(strModelName) - 7), "\"))

                                                                         End If

                                                                            swModel.ForceRebuild3 (False) 

                                                                            swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings 

                                                                            swApp.CloseDoc swModel.GetTitle 

                                                                            Set swModel = Nothing 

                                                                        End If 

                                                                        sFileName = Dir 

                                                                    Loop 

                                                                End Sub

                                                                 

                                                                 

                                                                • Re:  Batch generated description name from the file name
                                                                  Peter Persson

                                                                  SOLVED.... THANKS Leon and Deepak....