wxMkDir return false even if it successfully creates the directory. 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
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 360
Joined: Tue Oct 20, 2020 3:35 pm

wxMkDir return false even if it successfully creates the directory.

Post by apoorv569 » Sat Jul 17, 2021 9:51 pm

I'm trying to check if a directory exists, and if not I want to make that directory, so I wrote this,

Code: Select all

#define CONFIG_DIR wxStandardPaths::Get().GetUserConfigDir() + "/.config/SampleHive"

    if (!wxDirExists(CONFIG_DIR))
    {
        if (!wxMkDir(CONFIG_DIR, wxPOSIX_USER_READ | wxPOSIX_USER_WRITE | wxPOSIX_USER_EXECUTE |
                     wxPOSIX_GROUP_READ | wxPOSIX_GROUP_EXECUTE |
                     wxPOSIX_OTHERS_READ | wxPOSIX_OTHERS_EXECUTE))
        {
            wxMessageBox(wxString::Format(_("Error! Could not create directory %s"), CONFIG_DIR), _("Error!"),
                         wxOK | wxCENTRE, this);
        }
    }
The does work, and it creates the directory,
Image
but it still shows the error, the first time I open the app, after that it doesn't. Is this is a bug, or is there something that I missed?
Image

User avatar
doublemax
Moderator
Moderator
Posts: 16381
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMkDir return false even if it successfully creates the directory.

Post by doublemax » Sun Jul 18, 2021 12:01 am

Use wxFileName::Mkdir with flag wxPATH_MKDIR_FULL. Then you don't need the wxDirExists.
https://docs.wxwidgets.org/trunk/classw ... 560e1a62c7
Use the source, Luke!

apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 360
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxMkDir return false even if it successfully creates the directory.

Post by apoorv569 » Sun Jul 18, 2021 4:47 am

doublemax wrote:
Sun Jul 18, 2021 12:01 am
Use wxFileName::Mkdir with flag wxPATH_MKDIR_FULL. Then you don't need the wxDirExists.
https://docs.wxwidgets.org/trunk/classw ... 560e1a62c7
This works, thanks.

apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 360
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxMkDir return false even if it successfully creates the directory.

Post by apoorv569 » Tue Jul 20, 2021 6:37 am

doublemax wrote:
Sun Jul 18, 2021 12:01 am
Use wxFileName::Mkdir with flag wxPATH_MKDIR_FULL. Then you don't need the wxDirExists.
https://docs.wxwidgets.org/trunk/classw ... 560e1a62c7
Is there a function to delete a file in wxFileName? I could not find one, there is RmDir(), can it also delete files? Currently I'm doing like this,

Code: Select all

void MainFrame::OnSelectResetAppData(wxCommandEvent& event)
{
    wxMessageDialog clearDataDialog(this, wxString::Format(_("Warning! This will delete configuration file %s and database %s permanently, "
                                                             "are you sure you want to delete these files?"), m_ConfigFilepath, m_DatabaseFilepath),
                                    "Clear app data", wxYES_NO | wxNO_DEFAULT | wxCENTRE, wxDefaultPosition);

    switch (clearDataDialog.ShowModal())
    {
        case wxID_OK:
            if (wxRemoveFile(m_ConfigFilepath) && wxRemoveFile(m_DatabaseFilepath))
            {
                wxMessageBox(_("Successfully cleared app data"), _("Success"), wxOK | wxCENTRE, this);
            }
            else
            {
                wxLogDebug("Could not clear app data");
            }
            break;
        case wxID_NO:
            break;
        default:
            break;
    }
}
This works it deletes both files, but the wxMessageBox does not show up, neither the wxLogDebug in the else statement.

apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 360
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxMkDir return false even if it successfully creates the directory.

Post by apoorv569 » Tue Jul 20, 2021 4:24 pm

doublemax wrote:
I can't get to your last reply, when I click on the notification, it says "The requested topic does not exist."

User avatar
doublemax
Moderator
Moderator
Posts: 16381
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxMkDir return false even if it successfully creates the directory.

Post by doublemax » Tue Jul 20, 2021 6:35 pm

apoorv569 wrote:
Tue Jul 20, 2021 4:24 pm
I can't get to your last reply, when I click on the notification, it says "The requested topic does not exist."
That's because i deleted it.

wxRemoveFile should work fine and the code looks ok. One of the two paths must be taken, try a debugger to find out which one.
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 3087
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxMkDir return false even if it successfully creates the directory.

Post by PB » Tue Jul 20, 2021 9:22 pm

You create clearDataDialog with wxYES_NO style but delete the files when its ShowModal() returns wxID_OK. I wonder how that can work.

I assume you are aware that the user probably will not see the message of wxLogDebug(), unless it works differently on Linux than on Windows (Debug vs Release builds).

apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 360
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxMkDir return false even if it successfully creates the directory.

Post by apoorv569 » Wed Jul 21, 2021 1:37 pm

PB wrote: You create clearDataDialog with wxYES_NO style but delete the files when its ShowModal() returns wxID_OK. I wonder how that can work.
Ah! Yes, this was the problem, I was checking for wxID_OK instead of wxID_YES.
PB wrote: I assume you are aware that the user probably will not see the message of wxLogDebug(), unless it works differently on Linux than on Windows (Debug vs Release builds).
Yes, I am aware of this, the wxLogDebug() statements are for me only. If I want to show something to user, I use wxInfoBar or wxMessageBox.

Post Reply