Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:45:55+00:00 2026-05-27T11:45:55+00:00

What is the simplest way to get the file name that from a path?

  • 0

What is the simplest way to get the file name that from a path?

string filename = "C:\\MyDirectory\\MyFile.bat"

In this example, I should get “MyFile”. without extension.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:45:56+00:00Added an answer on May 27, 2026 at 11:45 am

    _splitpath should do what you need. You could of course do it manually but _splitpath handles all special cases as well.

    EDIT:

    As BillHoag mentioned it is recommended to use the more safe version of _splitpath called _splitpath_s when available.

    Or if you want something portable you could just do something like this

    std::vector<std::string> splitpath(
      const std::string& str
      , const std::set<char> delimiters)
    {
      std::vector<std::string> result;
    
      char const* pch = str.c_str();
      char const* start = pch;
      for(; *pch; ++pch)
      {
        if (delimiters.find(*pch) != delimiters.end())
        {
          if (start != pch)
          {
            std::string str(start, pch);
            result.push_back(str);
          }
          else
          {
            result.push_back("");
          }
          start = pch + 1;
        }
      }
      result.push_back(start);
    
      return result;
    }
    
    ...
    std::set<char> delims{'\\'};
    
    std::vector<std::string> path = splitpath("C:\\MyDirectory\\MyFile.bat", delims);
    cout << path.back() << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the simplest way to get the directory that a file is in?
What is the simplest way to get the directory that a file is in?
What is the simplest way to identify and separate GET and POST parameters from
I need the simplest way from C using Win32 to get the process handle
How could I get the image file path of a sprite in string form
What is the simplest way to get the NT-ID of a user in a
Whats the simplest way to get a barebones linux server installed? barebones = just
What's the simplest way to get XmlSerializer to also serialize private and public const
In a C++ Linux app, what is the simplest way to get the functionality
Inspired by the question What’s the simplest way to call Http GET url using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.