Need some help with wxExecute 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
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Need some help with wxExecute

Post by Nico » Mon Mar 18, 2013 6:38 pm

Hello,

Using wxWidgets-2.8.11 on windows XP (no need for portability in this case) I am trying to get my program to start a vb-script file that requires two arguments when it is done. I have tried quite a couple of possible ways to do create a string with the command name and the arguments for wxExecute, but I have to admit I just don't really understand how I can use wxExecute.

The arguments required are a string with the path to the directory where a file can be found and a string with the name of the file.

This is how I am trying to do it now:

Code: Select all

wxExecute(wxString::Format(_("clean_logfile.vbs  \"%s\" \"%s\" NULL"), wxFileName(fileName).GetPath().mb_str(), wxFileName(fileName).GetFullName().mb_str()));
Where:
- clean_logfile.vbs is the vb-script that is stored in the same directory as my executable
- fileName is a wxString with the full name of the file I want the script to handle.

The vb-script works if I call it with this command in a bat file: clean_logfile.vbs "." "Logfile_20130314_163010.dat.130315161044.csv" and the specified file is also present in the same directory. Obviously I don't want to be restricted to the same directory and this specific filename, so I can't hard code it, so I used wxWidgets to simply browse to a file and select it. This is how I get the fileName variable.

If there is anyone who knows what I should change or could try the advise would be greatly appreciated.

Kind regards, Nico

DavidHart
Site Admin
Site Admin
Posts: 3888
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: Need some help with wxExecute

Post by DavidHart » Mon Mar 18, 2013 7:46 pm

Hi Nico,
wxString::Format(_("clean_logfile.vbs
_() marks a string as being for translation. You really don't want that here. Use wxT() instead.
Obviously I don't want to be restricted to the same directory and this specific filename
I'm not sure I understand. Does the wxExecute code work in a particular directory? Or do things work only outside wx?

Two suggestions:
1) You presumably need to pass the full path to the clean_logfile.vbs script, unless you've changed dir programmatically, or it's guaranteed to be in your PATH.
2) Until it works, I strongly suggest you compose your command string separately, then pass it to wxExecute. That way you can examine (in a debugger or wxMessageBox or...) the command you're going to be using, then copy and test exactly that string inside the .bat file. Doing so means you can get the command right first, then worry about wxExecute second.

Regards,

David

Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Re: Need some help with wxExecute

Post by Nico » Mon Mar 18, 2013 8:06 pm

Hello David,

Thank you very much for your quick reply.
DavidHart wrote:_() marks a string as being for translation. You really don't want that here. Use wxT() instead.
A good point, I will change that immediately.
DavidHart wrote:
Obviously I don't want to be restricted to the same directory and this specific filename
I'm not sure I understand. Does the wxExecute code work in a particular directory? Or do things work only outside wx?
So far, I was unable to run the script from wxWidgets in any of the setups I tried.
Maybe I was not clear I wanted to explain my intention. I want to make a tool to help me process the logfiles from an application. These logfiles are dat-files stored with a new name every half hour and in a new directory every day. I want to install my tool in one place (C:\Program files\myTool) and when I start it I want to be able to select the logfile I need, so far it all works. When my tool is done converting the file it stores the result as a csv-file in the same directory as where the dat-file came from, so far still no problems. Then I want to call the vb-script to clean up the csv-file and fix some formatting, but the vb-script is in C:\Program files\myTool and won't be able to find the csv-file. So I need to tell it in which directory the csv-file is and what its name is (the name includes a timestamp to prevent duplicates, so it is different every time).

DavidHart wrote: 1) You presumably need to pass the full path to the clean_logfile.vbs script, unless you've changed dir programmatically, or it's guaranteed to be in your PATH.
The script is not in my path file, I figured putting it in the same folder as the executable would do but a full path would be better. Is there a way to ask the application what its full path is (as this would be the same for the script)? I tried moving the script to C:\ and calling it with the full path, but it did not work. I will now restore that so i can at least rule that problem out.
DavidHart wrote: 2) Until it works, I strongly suggest you compose your command string separately, then pass it to wxExecute. That way you can examine (in a debugger or wxMessageBox or...) the command you're going to be using, then copy and test exactly that string inside the .bat file. Doing so means you can get the command right first, then worry about wxExecute second.
This seems like a good plan. Thank you very much for the tip.

Kind regards, Nico

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

Re: Need some help with wxExecute

Post by doublemax » Mon Mar 18, 2013 9:08 pm

A .VBS cannot by executed by itself by the system. The shell is responsible for finding the program that's associated with that filetype. wxExecute doesn't do that.

You'll need something like this:

Code: Select all

wxMimeTypesManager mtm;

wxFileType *ftype = mtm.GetFileTypeFromExtension(wxT("vbs"));
if(ftype!=NULL) {
	wxString opencommand = ftype->GetOpenCommand( wxT("E:\\path\\to\\your\\file.vbs") );
	wxLogMessage(opencommand);
	//wxExecute(opencommand);
	delete ftype;
}
Use the source, Luke!

Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Re: Need some help with wxExecute

Post by Nico » Tue Mar 19, 2013 12:43 pm

Thank you doublemax that helped a great deal. Like Dave mentioned I added the full path to the script, I would like for that to be a variable though.
I am now able to execute the script, but I am still unable to pass it the required arguments.
Is there anyone who knows how to do that?

Code: Select all

    wxFileType *ftype = wxTheMimeTypesManager->GetFileTypeFromExtension(wxT("vbs"));
    if(ftype == NULL)
    {
         wxMessageBox(wxString::Format(_("Can't find default program for extension 'vbs'")));
         return -1;
    }
    //wxString opencommand = opencommand = (wxString::Format(wxT("%s  \"%s\" \"%s\" NULL"), cmd, wxFileName(fileName).GetPath().mb_str(), wxFileName(fileName).GetFullName().mb_str()));
    wxString opencommand = (wxT("c:\\clean_logfile.vbs"));
    cmd = ftype->GetOpenCommand( opencommand );
    delete ftype;
    wxExecute(opencommand);
Kind regards, Nico

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

Re: Need some help with wxExecute

Post by doublemax » Tue Mar 19, 2013 2:26 pm

Just append the other parameter to the whole open command.

The result should look like this:

Code: Select all

c:\windows\system32\wscript.exe "c:\clean_logfile.vbs" "d:\mylogfile.txt"
Use the source, Luke!

Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Re: Need some help with wxExecute

Post by Nico » Wed Mar 20, 2013 11:16 am

Maybe this is not the most effective way, but I got it to do what I wanted.

I tried to add the arguments before asking the file system to create the appropriate command, where I should have appended them afterward. In addition I made some mistakes in converting the output of the wxFileName object and finally I had to clean the command created by the file system, because it put an astrix in the place of the arguments (probably this could have been prevented some how, but this fix is acceptable for me).
Once I managed to get the path of the csv file in a usable way I was also able to get the executables location, so I was able to move the script from C:\ to the executables folder.

Thank you very much for all the help.

Kind regards, Nico

Code: Select all

    wxFileType *ftype = wxTheMimeTypesManager->GetFileTypeFromExtension(wxT("vbs"));
    if(ftype == NULL)
    {
         wxMessageBox(wxString::Format(_("Can't find default program for extension 'vbs'")));
         return -1;
    }
    wxString thisExecutablesPath = wxStandardPaths::Get().GetDataDir();;
    wxString opencommand = (wxString::Format(wxT("%s\\clean_logfile.vbs"), thisExecutablesPath.c_str()));
    cmd = ftype->GetOpenCommand( opencommand );
    cmd.Replace(wxT("*"),wxT(""));
    cmd.Trim(true);
    cmd = (wxString::Format(wxT("%s \"%s\" \"%s\""), cmd.c_str(), wxFileName(fileName).GetPath().c_str(), wxFileName(fileName).GetFullName().c_str()));
    // wxMessageBox(wxString::Format(_("%s"), (const char*) cmd.c_str()));
    delete ftype;

Post Reply