CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    121

    Help on replacing part of a string

    Path path(ToString(backUpPath));
    VRIniInfo vrIniInfo = theApp.GetVRIniInfo();
    CString lessonPath = vrIniInfo.GetLessonBasePath();
    CString reponsePath = vrIniInfo.GetResponseBasePath();
    CString destLesson = path.AppendToPath("Lavac\record.sav");
    CString destResp = path.AppendToPath("Lavac\LECONS");

    For example

    lessonPath = C:\Lavac\LECONS
    reponsePath = C:\Lavac\record.sav
    destLesson = G:\temp\Lavac\LECONS
    destResp = G:\temp\Lavac\record.sav

    I need to be able to replace C:\ by G:\temp

    Does anyone have a suggestion?

    Thank You


  2. #2
    Join Date
    May 1999
    Location
    Bogotá, Colombia
    Posts
    37

    Re: Help on replacing part of a string

    Hows about this:


    char path_buffer[_MAX_PATH];
    //path_buffer contains the full pathname of your file

    char new_path_buffer[_MAX_PATH];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char fname[_MAX_FNAME];
    char ext[_MAX_EXT];

    _splitpath( path_buffer, drive, dir, fname, ext );
    _sprintf( new_path_buffer, "g:\\temp\\%s%s", fname, ext );
    printf("New path is %s\n", new_path_buffer );





  3. #3
    Join Date
    Apr 1999
    Posts
    383

    Re: Help on replacing part of a string

    If you've got a recent version of MFC you can use CString::Replace():

    CString oldPath = "C:\\path1\\path2";
    oldPath.Replace("C:\\", "G:\\temp\\");

    else if you can bear to link with Shlwapi.lib, you can use the shell utility path functions, especially PathSkipRoot():

    #include "Shlwapi.h"

    CString newRoot = "G:\\temp\\";
    CString oldPath = "C:\\path1\\path2";
    CString newPath = newRoot + PathSkipRoot(oldPath);

    else it's just primitive CString stuff:

    CString newRoot = "G:\\temp\\";
    CString oldPath = "C:\\path1\\path2";
    oldPath = newRoot + oldPath.Right(oldPath.GetLength() - 3);

    [note: the '\\' is one character]

    Dave



Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured