Page 1 of 1

FileName$ AND Parameter$ in one parameter for RunProgram?

Posted: Thu Feb 22, 2007 7:30 pm
by merihevonen
Hello!

It would be alot easier if I could just pass the FileName$ along with the Parameter$ in one parameter instead of two in RunProgram..

So something like this:
RunProgram(Command$ [, WorkingDirectory$ [, Flags [, SenderProgram]]])
Instead of this:
RunProgram(FileName$ [, Parameter$, WorkingDirectory$ [, Flags [, SenderProgram]]])

Right now I am using this (probably very bad) alternative way:

Code: Select all

Command$="example-program parameter1 parameter2"
If Trim(FindString(Command$, " ", 0))<>0 ; If there are spaces (probably having a parameter next to them)..
 CommandPos=FindString(Command$, " ", 0)-1 ; See where the first space is..
 ACommand$=Trim(Left(Command$, CommandPos))  ; Cut only the FileName$ part of the Command$...
 BCommand$=Trim(Mid(Command$, CommandPos+2, Len(Command$)-CommandPos))  ; Now cut the parameters from Command$..
Else ; If there are no spaces..
 ACommand$=Command$ ; The future FileName$ will be Command$
 BCommand$="" ; And the parameter part will be blank..
EndIf
RunProgram(ACommand$, BCommand$, "", #PB_Program_Read|#PB_Program_Open) ; Run the program.. (duh)
This would be extremly useful if you need to execute a command that the user inputs somewhere in your application.. For example this: http://www.purebasic.fr/english/viewtopic.php?t=25966

Re: FileName$ AND Parameter$ in one parameter for RunProgram

Posted: Thu Feb 22, 2007 8:55 pm
by PB
RunProgram is a wrapper for the ShellExecute API, so I doubt it'll change.

Anyway, this seems easier to me for what you want to accomplish:

Code: Select all

Command$="example-program parameter1 parameter2"
;Command$="example-program" ; No parameters.
sp=FindString(Command$," ",1)
ACommand$=Left(Command$,sp-1)
BCommand$=Mid(Command$,sp+1,Len(Command$))
If ACommand$="" : Swap ACommand$,BCommand$ : EndIf
Debug ACommand$
Debug BCommand$

Posted: Thu Feb 22, 2007 9:33 pm
by ts-soft
You can use the WinExec API

Code: Select all

WinExec_("notepad E:\Eigene Dateien\BASIC.txt", #SW_SHOWNORMAL)

Posted: Thu Feb 22, 2007 9:35 pm
by merihevonen
ts-soft wrote:You can use the WinExec API

Code: Select all

WinExec_("notepad E:\Eigene Dateien\BASIC.txt", #SW_NORMAL)
Linux? :?

EDIT: Thanks PB for the code!