wxFileName relative and absolute path Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
beneficii
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Nov 27, 2009 2:49 am

wxFileName relative and absolute path

Post by beneficii »

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?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFileName relative and absolute path

Post by doublemax »

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() );
Use the source, Luke!
beneficii
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Nov 27, 2009 2:49 am

Re: wxFileName relative and absolute path

Post by beneficii »

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() );
OK. That makes sense. So I could do, slightly modifying your code:

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() );
And then the rest of the code would work like yours! Thanks! It's perfect! Thanks again for another helpful answer!
beneficii
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Nov 27, 2009 2:49 am

Re: wxFileName relative and absolute path

Post by beneficii »

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;
}
Post Reply