Click to See Complete Forum and Search --> : Help on replacing part of a string


Sophie
May 11th, 1999, 09:30 AM
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

Andrew
May 11th, 1999, 10:40 AM
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 );

Dave Lorde
May 12th, 1999, 07:33 AM
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