Passing quotation marks to ShellExecute?

Started by jmburton2001, May 24, 2018, 12:25:19 PM

Previous topic - Next topic

jmburton2001

Hello!

I can run the following from a command prompt and it returns exactly what I want:

     psloglist.exe -d 5 "Media Center" -o EPG123 -s -t /t >C:\EVT_Test\Test.txt

I've tried "Run", "RunShell" and "ShellExecute" and they all return the same error. I'm thinking it has something to do with the quotation marks passed in the program parameters??

This is a partial snippet of my script:
   HomeLoc = ""H:\Public Transfer\"          <- Note: psloglist.exe is located here.
   psLogParam = "-d 5 ""Media Center"" -o EPG123 -s -t /t >C:\EVT_Test\Test.txt"
   ShellExecute ("%HomeLoc%psloglist.exe %psLogParam%","", "", @NORMAL, "")
      or
   ShellExecute ("%HomeLoc%psloglist.exe", psLogParam, "", @NORMAL, "")

And this is what's returned in the error box (3068: Function syntax, illegal delimiter):
   ShellExecute ("H:\Public Transfer\psloglist.exe -d 5 "Media Center" -o EPG123 -s -t /t >C:\EVT_Test\Test.txt","", "", @NORMAL, "")

The command line and parameters appear to be passed correctly and if I copy psloglist.exe -d 5 "Media Center" -o EPG123 -s -t /t >C:\EVT_Test\Test.txt out of the error box above and paste it into a command prompt, it works as intended.

Thank you for your advice and assistance!

td

Where to begin...

Regarding coding style, there is no reason to be using substitution in the above script.  Substitution has its place but that place is not when other language syntax works just as well or better.   The improper use of substitution can lead to buggy and difficult to debug scripts.   Why make life harder for yourself?

The error is the result of not properly handling quotes and placing the parameter values in the parameter intended for the executable's name.

Your general problem has to do with attempting use the output redirection operation (>) out of context.   The redirection operator is implemented in the Windows command shell and not in your tool or WinBatch.  This means you need to use the command shell if you want to use redirection.

Consider the following:
Code (winbatch) Select
strCmd = Environment('comspec')
strOut = 'c:\temp\dump.txt'
ShellExecute(strCmd, '/c dir > ': strOut, 'c:\temp', 0,'')
Run('Notepad.exe', strOut)

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jmburton2001

Quote from: td on May 24, 2018, 01:44:57 PM
Where to begin...

Under the topic of general forum etiquette, please don't just say you got an error.  If you are receiving a WinBatch error, take the time to indicate the error number and the error line.

I apologize for not making that clearer. I specified the error number and the error line in the "And this is what's returned in the error box" section.

Quote from: tdRegarding coding style, there is no reason to be using substitution in the above script.  Substitution has its place but that place is not when other language syntax works just as well or better.   The improper use of substitution can lead to buggy and difficult to debug scripts.   Whey make life harder for yourself?

I'd like to make my life easier. That's why I'm using Winbatch.

Quote from: tdYour specific problem likely has to do with attempting use the output redirection operation (>) out of context.   The redirection operator is implemented in the Windows command shell and not in your tool or WinBatch.  This means you need to use the command shell if you want to use redirection.

Consider the following:

I don't know what this refers to (and is totally foreign to me) so I'll fumble around with it and probably ask more questions later.

Question: I guess what I was trying to do was run this command (psloglist.exe -d 5 "Media Center" -o EPG123 -s -t /t >C:\EVT_Test\Test.txt) like I can from a command prompt. Is it possible to do that in a simple way?

td

You responded to my post before I finished re-editing it... I was distracted by other events and inadvertently posted before I was done.

You will likely experience a brief period of discomfort while breaking yourself of the habit of typing the percent (%) character every 10 letters or so.  But in the long run, your WinBatch scripts will make your life easier than they do now.
.
Did you look at the example?   It illustrates how to call a command from the Windows command shell so that you can use redirection.  You can do the same with your "psloglist.exe" program.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jmburton2001

Quote from: td on May 24, 2018, 02:22:47 PM
You responded to my post before I finished re-editing it... I was distracted by other events and inadvertently posted before I was done.

Please accept my apologies!

Quote from: tdYou will likely experience a brief period of discomfort will breaking yourself of the habit of typing the percent (%) character every 10 letters or so.  But in the long run, your WinBatch scripts will make your life easier than they do now.

I try not to use substitution but it seems like the majority of the errors I suffer are of the "Uninitialized Variable" variety. Therefore, I tend to use variables a lot. As I've mentioned previously, I'm definitely not a programmer. We used Winbatch pretty heavily back in the late 90's and early 00's. I remembered what a Godsend it was for us and have used it for small projects over the years. Recently I've wanted to tinker with it again and what takes you 5 minutes (or less) takes me 5 hours (or more). I just keep plugging along because my recollections are that Winbatch is really powerful and flexible. Even so, I'll plug along until I paint myself in a corner, Every. Single. Time...

Quote from: tdDid you look at the example?   It illustrates how to call a command from the Windows command shell so that you can use redirection.  You can do the same with your "psloglist.exe" program.

I did. It gave me fits until I used the following:
HomeLoc = DirScript()
Days = "2"

strCmd = Environment('comspec')
strOut = "C:\EVT_Test\Test.txt"
ShellExecute (strCmd, '/c psloglist -d %Days% "Media Center" -o EPG123 -s -t /t > %strOut%', HomeLoc, 0, '')


I wasn't using the "/c" switch because when I pasted it into a command prompt it gave an error so I stripped it out and it worked at the command prompt. The article you linked lined me out on that one.

The other thing was the redirect to the output file. The ": strOut" would not work. It would open a command prompt to the home directory and just sit there as though it were waiting for commands. I started painting myself into a corner again, so I stopped and broke it down into what worked at the command prompt and where it was hanging in the script. Every time it would error on the ShellExecute line it was the  3068: Function syntax, illegal delimiter error until it was written as shown above.

When I put the "> %strOut%" inside the quotation marks, it worked perfectly.

Thank you for taking the time to share your wisdom with me and a special thanks for your patience!


td

Glad you figured it but you don't need to use substitution instead of the concatenation operator as a target.  I can't tell you why it didn't work for you because I didn't see the script as you had written it but I use concatenation for that purpose frequently.

As far as not being a programmer goes.  Most WinBatch users are not professional programmers although we have many who are.   You certainly don't need to be a programmer to be a successful WinBatch script writer.  You just need to be someone that likes to learn something new once in an awhile. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade