Help trimming off filename from path

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

    #1

    Help trimming off filename from path

    I've got a form where i need to pull both the directory of a filename
    and the directory with the filename of an .inf file... I figured the
    easiest way to do this was to have an <input id=test type=file
    runat=server> control for the user to find the .inf file, and then make
    vb cut off the filename upon submission of the form. I just can't
    figure out how to trim off the filename leaving just the directory it's
    located in.


    just as a test i'm trying to display the content as follows

    Label1.Text = formINFLocation .Value.Trim(for mDriverINF.File Name)

    I did this hoping to dynamically trim off any inf filename that is
    chosen... though it seems that the Trim function does not work the way
    i was assuming it did. Is there another way to remove the filename from
    this location? I have a hunch it's far more complicated than my simple
    idea here. :D

  • Herfried K. Wagner [MVP]

    #2
    Re: Help trimming off filename from path

    "PvtPile" <derek.peldo@gm ail.com> schrieb:[color=blue]
    > I've got a form where i need to pull both the directory of a filename
    > and the directory with the filename of an .inf file... I figured the
    > easiest way to do this was to have an <input id=test type=file
    > runat=server> control for the user to find the .inf file, and then make
    > vb cut off the filename upon submission of the form. I just can't
    > figure out how to trim off the filename leaving just the directory it's
    > located in.[/color]

    Check out the 'System.IO.Path ' class and its shared methods.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • iwdu15

      #3
      RE: Help trimming off filename from path

      try something along the lines of this:

      UNTESTED

      Label1.Text = strPath.Substri ng(0, strPath.LastInd exOf("\"))

      that should give you the directory with the last \

      ex:

      C:\SomeFolder\A notherFolder\

      --
      -iwdu15

      Comment

      • Mythran

        #4
        Re: Help trimming off filename from path


        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        news:%23ZDAERjb GHA.4932@TK2MSF TNGP03.phx.gbl. ..[color=blue]
        > "PvtPile" <derek.peldo@gm ail.com> schrieb:[color=green]
        >> I've got a form where i need to pull both the directory of a filename
        >> and the directory with the filename of an .inf file... I figured the
        >> easiest way to do this was to have an <input id=test type=file
        >> runat=server> control for the user to find the .inf file, and then make
        >> vb cut off the filename upon submission of the form. I just can't
        >> figure out how to trim off the filename leaving just the directory it's
        >> located in.[/color]
        >
        > Check out the 'System.IO.Path ' class and its shared methods.
        >
        > --
        > M S Herfried K. Wagner
        > M V P <URL:http://dotnet.mvps.org/>
        > V B <URL:http://classicvb.org/petition/>[/color]

        Yeah, System.IO.Path. GetDirectoryNam e gets the directory path you want :)

        So...

        Dim dirName As String = System.IO.Path. GetDirectoryNam e(...)

        :)

        HTH,
        Mythran

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Help trimming off filename from path

          "iwdu15" <jmmgoalsteraty ahoodotcom> schrieb:[color=blue]
          > Label1.Text = strPath.Substri ng(0, strPath.LastInd exOf("\"))
          >
          > that should give you the directory with the last \
          >
          > ex:
          >
          > C:\SomeFolder\A notherFolder\[/color]

          Directory paths in Windows typically do not include a trailing backslash.
          In addition, note that not all systems necessarily use "\" as path separator
          character and thus your code won't work on these systems.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • PvtPile

            #6
            Re: Help trimming off filename from path

            Thanks iwdu15

            That worked great!

            I hate it when people just say "It worked, thanks." without showing us
            the results... so here it is.

            Dim pathname As String = FileUpload.Valu e
            Label1.Text = (FileUpload.Val ue.Substring(0,
            FileUpload.Valu e.LastIndexOf(" \")))

            Again, thanks for all the help on this topic!

            Comment

            • iwdu15

              #7
              Re: Help trimming off filename from path

              thats a good point...my code would only work on the systems that use the \
              path separator, thanks for the correction
              --
              -iwdu15

              Comment

              • iwdu15

                #8
                Re: Help trimming off filename from path

                glad i could help, just make sure you check Herfried's post above about your
                solution
                --
                -iwdu15

                Comment

                • PvtPile

                  #9
                  Re: Help trimming off filename from path

                  Indeed valid. The reason, howerver, is that i need this input is so
                  that i can use it in a shell command at a later date. The format that
                  was extracted was exactly what was needed. The directory will be used
                  to reference a file on an nt machine only. Great work guys... you guys
                  are too smart!

                  Comment

                  Working...