Announcement

Collapse
No announcement yet.

VB .Net question

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VB .Net question

    How can i turn this string:

    "c:\documents and settings\chris\desktop\test.txt"

    to this:

    strPath = "c:\documents and settings\chris\desktop\"

    strFilename = "test.txt"

    Cheers!
    Server 2000 MCP
    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

  • #2
    Hi TY!

    Do you want to output the path+filename together in a response.write?
    1 + 1 = 11 ... honest!

    Comment


    • #3
      'declare the variables
      Dim strPath as string
      Dim strFilename as string


      'imagine you have a textbox named strpath
      StrPath = strpath.text

      'imagine you have a text box named strfilename
      StrFilename = strfilename.text


      strpath = strpath + strfilename
      MCSE w2k
      MCSA w2k - MCSA w2k MESSAGING
      MCDBA SQL2k

      Comment


      • #4
        Re: VB .Net question

        Originally posted by tonyyeb
        How can i turn this string:

        "c:\documents and settings\chris\desktop\test.txt"

        to this:

        strPath = "c:\documents and settings\chris\desktop\"

        strFilename = "test.txt"
        Windux,

        I think you have it backwards. He's asking how to split A (parent) string into B and C (child) strings, which when combined again B+C = A, as seen in the example you've provided.

        -----

        Tony,

        How to do what you've asking all depends on how you're receving the expected input. This is just a psuedo-code example and may need to heavily modified for your needs - or may not work at all.

        Code:
        blah some input as string = "c:\documents and settings\chris\desktop\test.txt"
        dim strPath as string
        dim strFilename as string
        
        strPath = subString(blah, 0, 40)
        strFilename = subString(blah, 41, 8)
        If you don't know the length of the thing it will be more complicated. Especially if the path changes and/or the filename can have a random length.
        Andrew

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        Comment


        • #5
          OK

          Sorry guys

          TKS ahinson
          MCSE w2k
          MCSA w2k - MCSA w2k MESSAGING
          MCDBA SQL2k

          Comment


          • #6
            Hi guys

            The path and so the string size will vary as the path changes so i don't think that will work. Any other ideas?

            Cheers

            Chris
            Server 2000 MCP
            Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

            ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

            Comment


            • #7
              Originally posted by tonyyeb
              Hi guys

              The path and so the string size will vary as the path changes so i don't think that will work. Any other ideas?

              Cheers

              Chris
              Ok well what more can you tell me more about where the input is coming from? Perhaps we can figure out a way to make this work or another solution.

              Are you using the FolderBrowser or OpenFile dialog to get the path or is it user input?
              Is this a web or windows application?

              You could do this...

              Code:
              MyString= "C:\Documents and Settings\Chris\My Documents\My Pictures\Pr0n\blah.jpg"
              MyArray = Split(MyString, "\", -1, CompareMethod.Text)
              This will return an array of strings. The last element of the array is the filename, and all proceeding elements will compose the path. The array will look like the following:

              ' MyArray(0) contains "C:".
              ' MyArray(1) contains "Documents and Settings".
              ' MyArray(2) contains "Chris".
              ' MyArray(3) contains "My Documents".
              ' MyArray(4) contains "My Pictures".
              ' MyArray(5) contains "Pr0n".
              ' MyArray(6) contains "blah.jpg".

              Now you only need to reassemble the array into its component parts and you'll have what you need/want.

              Code:
              Imports System.Text [goes at the very top]
              
              Dim sb As New StringBuilder
              Dim FullPath As String = OpenFileDialog.Filename
              Dim MyArray(0) As String
              Dim strPath, strFilename As String
              
              MyArray = Split(FullPath, "\", -1, CompareMethod.Text)
              
              Dim i As Integer
              
              For i = 0 To MyArray.Length - 2
                     sb.Append(MyArray(i) & "\")
              Next i
              
              strPath = sb.ToString
              strFilename = MyArray(MyArray.Length - 1)
              Andrew

              ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

              Comment


              • #8
                Yeah its a Windows app and the input is from an openfiledialog using the openFD.filename value.
                Server 2000 MCP
                Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

                ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                Comment


                • #9
                  That code works, I just tested it for good measure.

                  I ended up with:

                  strPath = C:\Documents and Settings\Chris\My Documents\My Pictures\Pr0n\

                  strFilename = blah.jpg

                  Good luck,

                  Andrew
                  Andrew

                  ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                  Comment


                  • #10
                    Im getting the path right but the filename shows a number?

                    EDIT: Its working now... something went wrong in the copy and paste!!

                    Many thanks Andrew!
                    Server 2000 MCP
                    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

                    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                    Comment


                    • #11
                      No problem it was my pleasure. I'm glad you got it to work.
                      Andrew

                      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                      Comment

                      Working...
                      X