Extract file name from path

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

    Extract file name from path

    It's probably very simple... I would like to know if there is a function
    similar to ExtractFileName () (I've used it in Delphi), which returns only
    the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
    "MyFile.txt ".

    Thanks!

    - Kristoffer -


  • L#

    #2
    Re: Extract file name from path

    On Fri, 6 Feb 2004 13:47:57 +0100, "Kristoffer Persson" <hidden>
    wrote:
    [color=blue]
    >It's probably very simple... I would like to know if there is a function
    >similar to ExtractFileName () (I've used it in Delphi), which returns only
    >the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
    >"MyFile.txt" .
    >
    >Thanks!
    >
    >- Kristoffer -
    >[/color]

    System.IO.Path. GetFileName(str ing path);


    --
    Ludwig
    mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

    Comment

    • Vladimir Scherbina

      #3
      Re: Extract file name from path

      hm, I found no function that performs this. so I suppose this will help
      you...

      string GetFileName(str ing szPath)

      {

      string szRetVal = string.Empty;

      string szTmp = string.Empty;

      int j = szPath.Length - 1;

      while (szPath[j] != '\\')

      {

      szTmp += szPath[j];

      j--;

      }


      // you have the reversed file name

      // replace characters...


      int i = szTmp.Length;

      while (i != 0)

      {

      i--;

      szRetVal += szTmp[i];

      }

      szTmp = null;

      return szRetVal;

      }


      --
      /*
      Vladimir Scherbina,
      Ukraine, Kiev.
      */

      "Kristoffer Persson" <hidden> wrote in message
      news:#eHWk9K7DH A.2416@TK2MSFTN GP10.phx.gbl...[color=blue]
      > It's probably very simple... I would like to know if there is a function
      > similar to ExtractFileName () (I've used it in Delphi), which returns only
      > the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
      > "MyFile.txt ".
      >
      > Thanks!
      >
      > - Kristoffer -
      >
      >[/color]


      Comment

      • Roman S. Golubin

        #4
        Re: Extract file name from path


        Hi, Vladimir Scherbina!
        [color=blue]
        > hm, I found no function that performs this. so I suppose this will help
        > you...
        >
        > string GetFileName(str ing szPath)
        >
        > {
        >
        > string szRetVal = string.Empty;
        >
        > string szTmp = string.Empty;
        >
        > int j = szPath.Length - 1;
        >
        > while (szPath[j] != '\\')
        >
        > {[/color]

        I optimize your code ;-)

        string GetFileName(str ing szPath)
        {
        string[] s_arr = szPath.Split(ne w char[]{'\\'});
        return s_arr[s_arr.Length-1];
        }

        or

        string GetFileName(str ing szPath)
        {
        Regex r = new Regex(@"\w+[.]\w+$+");
        return r.Match(szPath) .Value;
        }


        --
        Roman S. Golubin
        ICQ UIN 63253392
        golubinr.ANTl_S SPAM@arhcity.ru


        Comment

        • Roman S. Golubin

          #5
          Re: Extract file name from path


          Hi, Kristoffer Persson!
          [color=blue]
          > It's probably very simple... I would like to know if there is a function
          > similar to ExtractFileName () (I've used it in Delphi), which returns only
          > the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
          > "MyFile.txt ".
          >[/color]

          string ExtractFileName (string szPath)
          {
          Regex r = new Regex(@"\w+[.]\w+$+");
          return r.Match(szPath) .Value;
          }


          --
          Roman S. Golubin
          ICQ UIN 63253392
          golubinr.ANTl_S SPAM@arhcity.ru


          Comment

          • Robert Sentgerath

            #6
            Re: Extract file name from path

            The static methods of Path give you a myriad of options to deal with file
            names that include the path.

            For example:

            string myFile = "C:\Temp\MyFile .txt";
            string filename = Path.GetFileNam e(myFile);

            You could also get the name without extension:

            filename = Path.GetFileNam eWithoutExtensi on(myFile);

            Just browse through all the different methods that Path gives you to see
            what else you can do.

            - Robert Sentgerath -


            Path.GetFileNam eWithoutExtensi on


            "Kristoffer Persson" <hidden> wrote in message
            news:%23eHWk9K7 DHA.2416@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > It's probably very simple... I would like to know if there is a function
            > similar to ExtractFileName () (I've used it in Delphi), which returns only
            > the file name from the full path, e.g. "C:\Temp\MyFile .txt" would return
            > "MyFile.txt ".
            >
            > Thanks!
            >
            > - Kristoffer -
            >
            >[/color]


            Comment

            Working...