Thread: Extracting only file name from file path

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    8

    Extracting only file name from file path

    Hey, do you know if there is any Win32 API for extracting various parts from a string containing the file name with the whole path, or do I have to figure out some algorithm myself?
    Or, if you have a code snippet at hand to do this work, could you paste it?

    Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Depending on how your path is stored there may be various things you can try. If it is a simple c-style character array, the strrchr function can be used to find the last '\\' character in the array. If you have a string container storing the whole path/filename, then the rfind member function can be used in the same way. Once you have that position, the filename is simply the string/array starting from just after that character and going on to the end. I'm not familiar enough with Win32 API calls to know if there is another way.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    Thanks, to retrieve the file name with extension, it goes as follows:

    Code:
    string f = "C:\\Windows\\Desktop\\picture.jpg";
    int i = f.find_last_of('\\');
    if (i != string::npos)
        f = f.substr(i+1); // f contains the result :)

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    WinApi solution:
    Code:
    TCHAR szPath[] = TEXT("c:\\dir1\\file.txt");
    PathStripPath(szPath);
    // Result: szPath == "file.txt"
    
    TCHAR szPath[] = TEXT("C:\\TEST\\sample.txt");
    PathRemoveFileSpec(szPath);
    // Result: szPath == "C:\TEST"
    Also, see the many other Path_ functions. Very useful.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    some win32 api path functions

    GetFileTitle-returns string system would display for filename
    GetFullPathName-given a filename returns full path for file.
    GetLogicalDriveStrings-returns strings for all valid drives
    GetLongPathName-converts shortpath to longpath
    GetShortPathName-converts longpath to short path

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM