Extract only the directory name from wxDirDialog::getPath

22 views
Skip to first unread message

Sean Mayard

unread,
Jun 8, 2022, 2:57:21 PM6/8/22
to wx-u...@googlegroups.com
I have a wxDirDialog and i am using it to get the full path of the choosen directory like:


dialog.GetPath();

But i want to extract only the directory (name) from this full path in a portable way. For example suppose i have a folder named myfiles inside a folder named Documents inside C drive like:

C:\Documents\myfiles

The above is the full path name that we get back from GetPath. But i want to extract the last part which is myfiles.

Is there a built in method for this. If not how can extract that last part out so that i can use it later. I mean i know that wxFileDialog has a builtin method GetFileName and GetDirectory, so i thought that wxDirDialog should also have a GetDirectory method but it doesn't seem so.

Gunter Königsmann

unread,
Jun 8, 2022, 3:27:54 PM6/8/22
to wx-u...@googlegroups.com, Sean Mayard
You can use wxFilename's method getdirs() for this.

Kind regards,
Gunter

Sean Mayard

unread,
Jun 9, 2022, 1:28:12 AM6/9/22
to Gunter Königsmann, wx-u...@googlegroups.com
I took your advice and it is working but I noticed one weird thing. In particular, in wxArrayString's documentation there is no mention of the method named back but I am still able to use back as shown below. In the documentation only the Last method is mentioned but not the back method. Why is this?

wxFileName dirname( dialog.GetPath(), "" );
lastPart = dirname.GetDirs().back(); //this works but documentation has no back method

As you can see I am able to use the `back` method even though there is no back method mentioned in the documentation of wxArrayString. Is the back method inherited from wxArray or something else.

david.l...@lanxess.com

unread,
Jun 9, 2022, 2:08:25 AM6/9/22
to wx-u...@googlegroups.com
wx-u...@googlegroups.com wrote on 09.06.2022 07:27:58:

> Von: "Sean Mayard" <seanm...@gmail.com>
> An: "Gunter Königsmann" <gunter.ko...@gmail.com>, wx-
> us...@googlegroups.com
> Datum: 09.06.2022 07:28
> Betreff: Re: Extract only the directory name from wxDirDialog::getPath
> Gesendet von: wx-u...@googlegroups.com
>
> I took your advice and it is working but I noticed one weird thing.
> In particular, in wxArrayString's documentation [docs.wxwidgets.org]
> there is no mention of the method named back but I am still able to use
back
> as shown below. In the documentation only the Last method is
> mentioned but not the back method. Why is this?
>
> wxFileName dirname( dialog.GetPath(), "" );
> lastPart = dirname.GetDirs().back(); //this works but documentation
> has no back method
>
> As you can see I am able to use the `back` method even though there
> is no back method mentioned in the documentation of wxArrayString.
> Is the back method inherited from wxArray or something else.

Hi Sean,

back() and Last() are both implemented directly in wxArrayString. Have a
look at arrstr.h if you like.

back() (and other functions like begin(), erase() etc.) mimic the
interface of C++ STL containers.
In fact if you did not change the compiler flag
wxUSE_STD_CONTAINERS_COMPATIBLY to 0 (default=1), wxArrayStrings are even
actually STL containers under the hood.

So by stating that wxArrayString can be used like an STL container, having
functions like back() etc. is to be expected.
However, Last() is an wxWidgets addition to the interface and therefore
worth mentioning in the documentation.

Have a look at this link:

https://docs.wxwidgets.org/3.1/overview_container.html

- David


> On Thu, 9 Jun 2022 at 00:57, Gunter Königsmann
<gunter.ko...@gmail.com
> > wrote:
> You can use wxFilename's method getdirs() for this.
>
> Kind regards,
> Gunter
> --
> Please read https://www.wxwidgets.org/support/mlhowto.htm
[wxwidgets.org]
> before posting.
> ---
> You received this message because you are subscribed to the Google
> Groups "wx-users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to wx-users+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/
> d/msgid/wx-users/
> CADD_VSQY4nLHFiGtUyvT8HFiN20mBsUhgfSKsJUSE4suDRjpYA%40mail.gmail.com
> [groups.google.com].
Reply all
Reply to author
Forward
0 new messages