Return folder name from file location path

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madlan
    New Member
    • Jun 2010
    • 6

    #1

    Return folder name from file location path

    Hi,
    How would I retrive the folder name a file is in?
    In this example I just want the highlighted folder name:

    C:\Program Files\FolderNameIWant\Application.ex e

    I can get the path excluding the file using various methods such as:

    Code:
    Dim intPos As Integer
    intPos = FileFullPath.LastIndexOfAny("\")
    intPos += 1
    Messagebox.show(FileFullPath.Substring(0, intPos))
    But cannot figure out how to return just the top level folder!

    Thanks.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Use string.split to separate on the '\' character
    The folder name will be the last element of the returned array.

    There are a number of methods in the System.IO.Path namespace that might make things easier on you as well.
    There are existing methods for GetFileName, RootPath, GetFileNameWith outExtension and so on

    Comment

    • madlan
      New Member
      • Jun 2010
      • 6

      #3
      I tried using Split but it seems to return the string in it's original format?

      strCustomerName .Split(New Char() {"\"c})
      MessageBox.Show (strCustomerNam e)

      I could not find anything in the System.Io.Path that would be of any use.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Then you didn't bother to read about how string.split works.

        Here is some general good advice. Don't just assume you know how a function will work based on its name and your guesswork. You could spend a lot of time dinking around with it, tons of trial and error and eventually figure out what it does. Or you could just go to the MSDN and read about and look at the explanation and samples.
        http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

        String.split does not affect or alter your original string. It is a method that returns an array of strings.

        string[] Yogi = string.split("c :\one\two\three ", '\');

        Will assign elements to the new array[] Yogi as follows
        [0] = c:
        [1] = one
        [2] = two
        [3] = three

        So if you take your total GetPathWithoutF ileName
        .split() it on the '\'
        the last element will be the folder name.

        Comment

        • madlan
          New Member
          • Jun 2010
          • 6

          #5
          Directory.Name does the job:

          Code:
          Dim filepath As String = "C:\Program Files\FolderNameIWant\cats\Application.exe"
                      Dim fi As New IO.FileInfo(filepath)
                      MessageBox.Show(fi.Directory.Name)
          Thanks for your advice.

          Comment

          • Softman
            New Member
            • Apr 2014
            • 1

            #6
            Code:
            1.Dim filepath As String = "C:\Program Files\FolderNameIWant\cats\Application.exe"
            2.             Dim fi As New IO.FileInfo(filepath)
            3.             MessageBox.Show(fi.Name)
            fi.name returns the Folder

            Comment

            • DaleDewing
              New Member
              • Oct 2014
              • 6

              #7
              Dale Dewing

              Name = Name.Substring( InStrRev(Name, "\"))

              Comment

              Working...