4 Replies Latest reply on Dec 16, 2017 5:54 AM by Bernhard Lang

    how to delete files on status change

    Bernhard Lang

      Hi experts,


      after I tried many codes from the web I finally have to ask if someone knows an answer here.


      I want to delete .pdf, .dxf and .dwg files which have been created on realease of a sldprt.

      When we now change the state to anything else e.g. in revision then all neutral file formats should be deleted from vault.

      Option Explicit
      Dim swApp As Object
      Dim strFilePath as String
      Dim oFSO as Object
      Sub main()
      Set swApp = Application.SldWorks
      strFilePath = "<Path>\<Filename>.pdf"
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      Debug.Print strFilePath
      If oFSO.FileExists("strFilePath") Then oFSO.DeleteFile "strFilePath"
      End Sub

      This will not work in a Task because I only have <path> and not the filename in here.

      But if I manually set the complete filename nothing happens.

      I also tried kill filename but it also doesn't work.

      I got stuck in here and I have no idea how I should set up a deletion of those files which are obsolete since the status change,

      When status changes to released/approved then the pdf will be generated new and old files will be overwritten with the PDF Task.


      Could anyone give a suggestion.


      Thank you in advance

        • Re: how to delete files on status change
          Charley Saint

          I think that will just delete the local copy of the file, not delete it from the vault. Here's the example code for deleting a file in EPDM, it's part of the API_GB.chm in C:\Program Files\SOLIDWORKS PDM


          'Delete file



                      Dim vault2 As IEdmVault7 = Nothing

                      If vault1 Is Nothing Then

                          vault1 = New EdmVault5()

                      End If

                      vault2 = DirectCast(vault1, IEdmVault7)

                      If Not vault1.IsLoggedIn Then

                          vault1.LoginAuto(VaultsComboBox.Text, Me.Handle.ToInt32())

                      End If


                      Dim pathList As EdmStrLst5

                      pathList = vault2.BrowseForFile(Me.Handle.ToInt32(), _

                                               EdmBrowseFlag.EdmBws_ForOpen + _

                                               EdmBrowseFlag.EdmBws_PermitVaultFiles + _

                                               EdmBrowseFlag.EdmBws_PermitMultipleSel, _

                                               "", "", "", "", _

                                               "Select Files to Delete")

                      If pathList Is Nothing Then Exit Sub


                      Dim pos As IEdmPos5

                      pos = pathList.GetHeadPosition

                      While Not pos.IsNull

                          Dim file As IEdmFile5

                          Dim folder As IEdmFolder5 = Nothing

                          file = vault2.GetFileFromPath(pathList.GetNext(pos), folder)

                          folder.DeleteFile(Me.Handle.ToInt32(), file.ID)

                      End While


                      Dim strCount As String

                      strCount = pathList.Count.ToString

                      MessageBox.Show("Deleted " + strCount + " file")


                  Catch ex As System.Runtime.InteropServices.COMException

                      MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message)

                  Catch ex As Exception


                  End Try

          • Re: how to delete files on status change
            Scott Stuart

            Charley is correct. Deleting the file via normal windows methods will only delete the local copy on whatever system the code is running on.


            However, you may find it easier to do what you're trying to do via a dispatch action.