I'm trying to make it so that I can move wxFileName into a particular directory, and then just set filenames relative to that directory. For example, I have wxFileName get a filename "C:\blah\blah.prj." I then would like to be able to set the relative filename (gotten from blah.prj) as "..\blah2\blah2.bin" to get the absolute filename of "C:\blah2\blah2.bin" when I call GetFullPath().
I'm seeing various functions, such as SetCwd(), MakeRelativeTo(), MakeAbsolute(), Assign(), SetPath(), but I'm not sure how to put them together to accomplish my purpose. SetCWD() does not seem to allow me to put paths relative to the working directory that was set.
Can anyone solve this riddle for me?
wxFileName relative and absolute path Topic is solved
Re: wxFileName relative and absolute path
I think wxFileName alone can't do it.
Try something like this:
Try something like this:
wxString base_dir( "C:\\blah\\" );
wxString rel_file( "..\\blah2\\blah2.bin" );
wxString pathname = base_dir + rel_file;
wxFileName fn( pathname );
fn.Normalize();
wxLogMessage( fn.GetFullPath() );
Use the source, Luke!
Re: wxFileName relative and absolute path
OK. That makes sense. So I could do, slightly modifying your code:doublemax wrote:I think wxFileName alone can't do it.
Try something like this:wxString base_dir( "C:\\blah\\" );
wxString rel_file( "..\\blah2\\blah2.bin" );
wxString pathname = base_dir + rel_file;
wxFileName fn( pathname );
fn.Normalize();
wxLogMessage( fn.GetFullPath() );
Code: Select all
wxFileName fn = _T("c:\\blah\\blah.prj");
// wxString base_dir = fn.GetPath(wxPATH_GET_SEPARATOR) //gets the ending separator; base_dir comes out equal to your example
//or even better
wxString base_dir = fn.GetPathWithSep();
wxString rel_file( "..\\blah2\\blah2.bin" );
wxString pathname = base_dir + rel_file;
fn.Assign( pathname );
fn.Normalize();
wxLogMessage( fn.GetFullPath() );
Re: wxFileName relative and absolute path
I could also write a handler function that does this for me automatically:
Code: Select all
wxFileName concat_dir(wxFileName original, const wxString& rel_path) {
original.Assign(original.GetPathWithSep() + rel_path);
original.Normalize();
return original;
}