Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Execute + Multiple Parameters 2

Status
Not open for further replies.

rennis

Programmer
Dec 6, 2006
80
CA
Hopefully someone here may be able to help. Is it possible to pass a second parameter into a batch file?
As you can see by the code below, 'theFirstParam' is a parameter with a string value that is being passed into a batch file. However, I would like to pass a second parameter into the batchfile as well.

So this is with one parameter (which works perfectly)
result = ShellExecute(hwnd, "open", "test.bat", theFirstParam, vbnullstring, 1)

but i want to pass a second parameter but the following statements don't work

First Attempt: (using plus sign)
result = ShellExecute(hwnd, "open", "test.bat", theFirstParam + theSecondParameter, vbnullstring, 1)

Second Attempt: (using '&')
result = ShellExecute(hwnd, "open", "test.bat", theFirstParam & theSecondParameter, vbnullstring, 1)
 
Have you tried using a space between theFirstParam and theSecondParameter?

Have you tried using straight Shell rather than ShellExecute?
 
Thank you HughLerwill for your reply.
I have tried the space between the parameters but the script wont execute properly.

I have used the shell, but I was reading that the shellexecute way/method is more reliable??

Thanks
 
ShellExecute is good when you have the name of a data file which is associated with a given app. Say you have a Word .doc file, pass the file name and ShellExecute will automatically fire up Word for you. Pass it a .txt file and it automatically fires up the default text processor which is usually NotePad but does not have to be.

Batch files (.BAT) are rather different because the OS treats them natively as executables along with .COM files and .EXE files. Shell should work fine for them.

regards Hugh,
 
Would there be any other way or a correct way i should say to pass the parameters using the shellexecute?

Thanks again
 
It is possible you could/ will get it to work with shellexecute but I would use Shell, have you tried it yet?

If you have and it still will not work come back with the code.
 
Would you know how i may be able to get it to work with shellexecute though? I have tried shell, it just seems sometimes its not as reliable in passing the parameters and the script running correctly...

Thanks again for your reply.
 
<Would you know how i may be able to get it to work with shellexecute though?>>

Post the exact code you are using and we shall see.
 
Heres the call to the shell

result = ShellExecute(hwnd, "open", "test.bat", parameter1, App.Path, 1)

 
It IS working for me with;

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command6_Click()

Dim Result As Long, parameter1$, parameter2$

parameter1$ = "rennis"
parameter2$ = "programmer"

Result = ShellExecute(Me.hwnd, "open", "test.bat", parameter1 & " " & parameter2, App.Path, 1)

MsgBox "reurned " & Result

End Sub

and this is the contents my test bat which takes two parameters;

echo %1 >file1.txt
echo %2 >file2.txt

Two files are created one containing 'rennis' and the other containing 'programmer'

Does the text in your parameters include spaces perhaps?
You may have to wrap them in double quotes if they do.

regards Hugh
 
Thank you very much. Greatly appreciate your assistance.
 
After trying the method above the & " " & concats the two parameters, it doesnt allow me to set them as seperate parameters

so say for instance parameter1 is test and parameter2 is test2,

instead of getting two separate parameters i get one string test test2.

Is there any way to separate them?
 
Tell me, when you use the command line to pass two parameters to the batch file, what do the parameters look like? I rather suspect they look like a single string ...
 
One has spaces, so i had to wrap it in double quotes like so:

Dim theString as String
theString = Chr(34) & txtTextBox1.Text

Where txtTextBox1.Text = C:\ Documents and Settings...

The other parameters are being read from a file, but do not have any spaces.
 
Be sure you have a space between the double quotes. I posted " " not "".
 
Problem fixed. I passed the parameter, that needed to be wrapped in quotes, last which passed the other parameters individually and fixed my problem....Thanks everyone for your help. Appreciate it muchly
 
All that and no pinkies.... [sad]


rennis,

Try clicking the link that says

Thank HughLerwill (and strongm)
for this valuable post!

It makes those who help have a [bigsmile]



I tried to have patience but it took to long! :) -DW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top