CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Cutting a string to remove the.exe in a full path

  1. #1
    Join Date
    Jul 2009
    Posts
    40

    Cutting a string to remove the.exe in a full path

    Hi all, first of thank you for taking your time to read this.

    I have a string which containts the full path of the application including the .exe

    Code:
    //getting working path
    	string PathToCut;
    
    	if( _argc >= 1 )
    	{
    		PathToCut = argv[0];
    	}
    	else
    	{
    		char pBuf[100];
    		GetModuleFileName(NULL, pBuf, 100);
    
    		PathToCut = pBuf;
    	}
    the code uses a function in case there is no arguments then ..

    it gives me

    C:\APPDIR\APPLICATION.EXE

    id like it to be

    C:\\APPDIR\\

    or

    C\\APPDIR

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,576

    Re: Cutting a string to remove the.exe in a full path

    _splitpath.

    Why do you want double back slashes?

  3. #3
    Join Date
    Jul 2009
    Posts
    40

    Re: Cutting a string to remove the.exe in a full path

    Quote Originally Posted by GCDEF View Post
    _splitpath.

    Why do you want double back slashes?
    Well first what is _splitpath ?? D:

    example of use storing it in a string please

    well I though it would be more safe .. its not a obligation.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,576

    Re: Cutting a string to remove the.exe in a full path

    Quote Originally Posted by dsylebee View Post
    Well first what is _splitpath ?? D:

    example of use storing it in a string please

    well I though it would be more safe .. its not a obligation.
    Do you not have access to MSDN or Google?

    It's not a matter of safe, it's a matter of right or wrong for your needs.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Cutting a string to remove the.exe in a full path

    I'm sure string::rfind and string::substr should solve this. Don't know if I'm 100% correct on the indexes, or if this is optimal...

    Code:
    string myOriginalString = "folder/file.exe";
    string myChoppedString = myOriginalString.substr(0, myOriginalString.rfind(".exe"));
    cout << myChoppedString;
    Something of this flavour.
    Last edited by monarch_dodra; November 25th, 2009 at 04:35 PM. Reason: typo

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Cutting a string to remove the.exe in a full path

    If you are using the Windows API anyway then you can call: PathRemoveFileSpec


    BTW, char pBuf[100]; should really be char pBuf[MAX_PATH];
    Last edited by Zaccheus; November 27th, 2009 at 11:56 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Cutting a string to remove the.exe in a full path

    Quote Originally Posted by dsylebee View Post
    it gives me

    C:\APPDIR\APPLICATION.EXE

    id like it to be

    C:\\APPDIR\\

    or

    C\\APPDIR
    Using the boost filesystem library this would be as simple as
    Code:
    #include <boost/filesystem.hpp>
    #include <iostream>
    
    int main()
    {
        boost::filesystem::path path("C:\APPDIR\APPLICATION.EXE");
        std::cout << path.branch_path().directory_string() << std::endl;
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)