Page 1 of 1

How to get filename and Dir name at the same time?

Posted: Mon Jul 29, 2013 2:20 am
by mekanik
Hi.. I have been looking around a way to get the File name and Dir name with a same dialog, in this case wxFileDialog. But hitting roadblocks all the way..
Below is not a elegant code but i was trying to get a hang of wxWidgets..
I tried using BeforeLast to get the Dir name but not sure how to make it a generic code ,i.e., make it work with all kinds of file name..
But then i am not sure how to get the file name alone..

Ok the aim is that i will get a input as a file but i need to separate the file name and dir name..

Code: Select all

void Test2Frame::OnOpen(wxCommandEvent& WXUNUSED(event))
{
    wxString file, fileDir, tempName;
    wxFileDialog* openFileDialog = new wxFileDialog(this, _("Open XML file"), "", "","XML files| *.xml", wxFD_OPEN|wxFD_FILE_MUST_EXIST);

    if (openFileDialog->ShowModal() == wxID_CANCEL)
        return;

    file = openFileDialog->GetPath();
        wxLogMessage("File output %s", file);

    wxFileInputStream input_stream(file);
    if (!input_stream.IsOk())
    {
        wxLogError("Cannot open file '%s'.", file);
            return;
    }
    //File size
    struct stat fileStat; stat(file, &fileStat);
    wxCharBuffer filepath = file.ToUTF8();
  //do something with the file.. 

    size_t pathLen = file.Len();
        wxLogMessage("File length %d", pathLen);

    wxLogMessage("BeforeLast %s", file.BeforeLast('a'));// get the Dir name but the file name needs to start with "a"
    TextCtrl1->AppendText(file.BeforeLast('a'));
Suggetsions please..

Re: How to get filename and Dir name at the same time?

Posted: Mon Jul 29, 2013 6:22 am
by doublemax

Code: Select all

#include <wx/filename.h>

wxFileName fname( openFileDialog->GetPath() );

wxLogMessage( wxT("filename=%s"), fname.GetFullName() );
wxLogMessage( wxT("path=%s"), fname.GetPath() );
wxLogMessage( wxT("path with separator=%s"), fname.GetPathWithSep() );