Extract path from path & filename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    Extract path from path & filename

    Hi,

    Using the code below I can extract the filename from a path but I would like
    to know how to get just the path too.
    So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far
    Away\SomeFilena me.txt"
    The filename is: SomeFilename.tx t
    The path only is: C:\A Long Time Ago\In A Galaxy\Far Far Away\

    I know there's other methods of getting the filename only, path only etc.
    that are new to VB.NET but they involve using the FileInfo/DirectoryInfo
    classes and for this I prefer to use the InStrRev/Trim method.

    Thanks,
    Paul

    Dim i As Integer
    Dim sFilename As String
    Dim file As String = "C:\A Long Time Ago\In A Galaxy\Far Far
    Away\SomeFilena me.txt"

    i = InStrRev(file.T rim, "\")
    If i = 0 Then
    'No path found, only filename
    sFilename = file.Trim
    Else
    'Assign filename only to variable
    sFilename = Right(file.Trim , Len(file.Trim) - i)
    End If

    MsgBox(sFilenam e)


  • Lawrence J. Rizzo

    #2
    Re: Extract path from path & filename

    You can use virtually the same code...just change to use the MID function,
    retunring the 1st character through the character before the first \ from
    the end.
    [color=blue]
    > 'Assign path only to variable
    > sPath = Mid(file.Trim, 1, i-1)[/color]



    "Paul" <me@home.com> wrote in message
    news:g5idnRyzy-EnjxHfRVnyuw@pi pex.net...[color=blue]
    > Hi,
    >
    > Using the code below I can extract the filename from a path but I would
    > like to know how to get just the path too.
    > So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far
    > Away\SomeFilena me.txt"
    > The filename is: SomeFilename.tx t
    > The path only is: C:\A Long Time Ago\In A Galaxy\Far Far Away\
    >
    > I know there's other methods of getting the filename only, path only etc.
    > that are new to VB.NET but they involve using the FileInfo/DirectoryInfo
    > classes and for this I prefer to use the InStrRev/Trim method.
    >
    > Thanks,
    > Paul
    >
    > Dim i As Integer
    > Dim sFilename As String
    > Dim file As String = "C:\A Long Time Ago\In A Galaxy\Far Far
    > Away\SomeFilena me.txt"
    >
    > i = InStrRev(file.T rim, "\")
    > If i = 0 Then
    > 'No path found, only filename
    > sFilename = file.Trim
    > Else
    > 'Assign filename only to variable
    > sFilename = Right(file.Trim , Len(file.Trim) - i)
    > End If
    >
    > MsgBox(sFilenam e)
    >[/color]


    Comment

    • Paul

      #3
      Re: Extract path from path &amp; filename

      Thanks Lawrence, that did the job nicely.


      "Lawrence J. Rizzo" <ljrconsult@ade lphia.net> wrote in message
      news:Kridna7vn4 gEihHfRVn-pw@adelphia.com ...[color=blue]
      > You can use virtually the same code...just change to use the MID function,
      > retunring the 1st character through the character before the first \ from
      > the end.
      >[color=green]
      >> 'Assign path only to variable
      >> sPath = Mid(file.Trim, 1, i-1)[/color]
      >
      >
      >
      > "Paul" <me@home.com> wrote in message
      > news:g5idnRyzy-EnjxHfRVnyuw@pi pex.net...[color=green]
      >> Hi,
      >>
      >> Using the code below I can extract the filename from a path but I would
      >> like to know how to get just the path too.
      >> So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far
      >> Away\SomeFilena me.txt"
      >> The filename is: SomeFilename.tx t
      >> The path only is: C:\A Long Time Ago\In A Galaxy\Far Far Away\
      >>
      >> I know there's other methods of getting the filename only, path only etc.
      >> that are new to VB.NET but they involve using the FileInfo/DirectoryInfo
      >> classes and for this I prefer to use the InStrRev/Trim method.
      >>
      >> Thanks,
      >> Paul
      >>
      >> Dim i As Integer
      >> Dim sFilename As String
      >> Dim file As String = "C:\A Long Time Ago\In A Galaxy\Far Far
      >> Away\SomeFilena me.txt"
      >>
      >> i = InStrRev(file.T rim, "\")
      >> If i = 0 Then
      >> 'No path found, only filename
      >> sFilename = file.Trim
      >> Else
      >> 'Assign filename only to variable
      >> sFilename = Right(file.Trim , Len(file.Trim) - i)
      >> End If
      >>
      >> MsgBox(sFilenam e)
      >>[/color]
      >
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: Extract path from path &amp; filename

        Paul,

        For that is the path class.


        I give you the member page the class page is for this in my opinion a little
        bit confusing.

        I hope this helps,

        Cor


        Comment

        • Chris Dunaway

          #5
          Re: Extract path from path &amp; filename

          Or you could use the shared methods of the Path class

          Comment

          • Paul

            #6
            Re: Extract path from path &amp; filename

            Thanks Chris and Cor




            "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
            news:OexHieEXFH A.3176@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Paul,
            >
            > For that is the path class.
            > http://msdn.microsoft.com/library/de...mberstopic.asp
            >
            > I give you the member page the class page is for this in my opinion a
            > little bit confusing.
            >
            > I hope this helps,
            >
            > Cor
            >[/color]


            Comment

            Working...