tags:

views:

188

answers:

4

I am running an executable in a batch file with two parameters;

cmd /k ""executable" "param1" "param2""

This returns a string that I want to launch. I can't figure out how to set this return in a variable and subsequently launch it in IE.

Any ideas?

A: 

start %1 %2

Daniel A. White
A: 

Edit: Romulo A. Ceccon posted a much better solution which doesn't involve any file system access and dirty tricks. Left this here for reference (it works with command.com as well if you need 9x compatibility), but please prefer Romulo's solution.


Go through an environment variable you set by using an intermediate helper script you dynamically generate from a template. You will need write permissions somewhere, otherwise it cannot be done (the Windows command shell language is very, very limited.)

Let's call your helper script template helper.tpl with the following contents:

set INTERMEDVAR=

Make sure that helper.tpl has only a single line (no trailing CRLF!) and make sure you don't have any spaces after the equals sign there.

Now, in your main script, capture the output from your command into a temporary file (let's call it my_output_file.tmp):

cmd /k ""executable" "param1" "param2"" > my_output_file.tmp

Then copy the contents of the helper template and the output together into your helper script, let's call it my_helper_script.cmd:

copy /b helper.tpl + my_output_file.tmp my_helper_script.cmd

Then evaluate the helper script in the current context:

call my_helper_script.cmd

Now the INTERMEDVAR variable is set to the first line of the output from "executable" (if it outputs more than one line, you're on your own...) You can now invoke IE:

start iexplore.exe "%INTERMEDVAR%"

And don't forget to clean up the created files:

del /q /f my_output_file.tmp my_helper_script.cmd

This will obviously not work when invoked multiple times in parallel - you'll have to parametrize the temporary file and helper script names using the current cmd.exe's PID (for example) so that they won't overwrite each other's output, but the principle is the same.

However, if you can get a real shell, use that. cmd.exe is extremely cumbersome.

Mihai Limbășan
+4  A: 

If the returned string contains a single line you may use FOR /F to set the value of an environment variable. For example:

s1.cmd

echo this is a one line string

s2.cmd

@SETLOCAL
@ECHO OFF
for /f "tokens=*" %%a in ('cmd /c s1.cmd') do set MY_VAR=%%a
echo got: %MY_VAR%
ENDLOCAL

Result

C:\> s2.cmd
got: this is a one line string

C:\>
Romulo A. Ceccon
Ohh, nice. Much, much better than my solution (which was a leftover from the days of command.com, methinks :D). Upvoted this.
Mihai Limbășan
+1  A: 

You can use the following syntax to capture the output of your executable into a variable:

FOR /F "tokens=*" %%i in ('%~dp0YOUR_APP.exe') do SET TOOLOUTPUT=%%i

Source

then you can pass the value on to IE like so:

START "YOUR_WINDOW_NAME" /MAX /D"C:\Program Files\Internet Explorer\" iexplore %TOOLOUTPUT%

I take it that the application code that determines the url is too complicated to be reproduced in a batch file directly, or the source to the executable has been lost. If not I personally would prefer to have the logic visible in the batch file itself.

Joshua Drake
Thank you Joshua, this worked perfectly.The application code was too difficult to reproduce in a batch file
kgbland