3 Replies Latest reply on Jun 22, 2016 9:34 PM by Viktor Bovzdarenko

    How to get the first directory from a file location

    Aj Bosma

      How do i get the first directory from a file path, where de directory is e.g.:   x:\16.001\Section\110

       

      By using:

       

      MyFolder = CurDir$

      MyPath1 = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") - 1)
      MyPath1 = Right(MyPath1, Len(MyPath1) - InStrRev(MyPath1, "\"))

       

      i can get 110.

      But i can't get to 16.001

       

      Any help would be welcome

        • Re: How to get the first directory from a file location
          Viktor Bovzdarenko

          Hi

          Using " Split (x:\16.001\Section\110, "\") " will give you an array :

          x:

          16.001

          Section

          110

          First 2 can be combined in the new string:

          Dim vPath As Variant

          vPath = Split (x:\16.001\Section\110, "\")

          Dim newStr As String

          newStr = vPath(0) & "\" & vPath (1)

          which will give: x:\16.001 or just vPath (1)  (which is 16.001) if necessary

          Regards

           

          • Re: How to get the first directory from a file location
            Aj Bosma

            Thank you for your responce, it worked...

            and with FilePath = swModel.GetPathName i even got is to work dynamicaly.

             

            Wonder if i can get 16.001 from the file path if it varies in location or has a added decription.

            eg.

            x:\16.001\Section\110

            x:\something\16.001\Section\110

            x:\something\something\16.001-ProjectName\Section\110

              • Re: How to get the first directory from a file location
                Viktor Bovzdarenko

                HI Aj

                I guess that in this situation additional conditions could be used. After the path is divided by "Split":

                     Check that the string is numeric:

                Dim numericCheck As Boolean: numericCheck = IsNumeric(testVar)

                or

                     Check if it contains any special characters:

                If inStrRev ("16.001", ".") > 0 Then

                ...

                End If

                or

                     Replace in the whole path some part (which you know) by empty characters:

                stringResult = Replace ("x:\something\something\16.001-ProjectName\Section\110", "x:\something\something\", "")

                 

                Anyway there always be quite a big chance of an error