从可执行文件设置变量

时间:2021-12-30 22:38:42

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

我在一个包含两个参数的批处理文件中运行可执行文件;

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

cmd / k“”可执行文件“”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.

这将返回我要启动的字符串。我无法弄清楚如何在变量中设置此返回值,然后在IE中启动它。

Any ideas?

4 个解决方案

#1


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:

然后你可以将值传递给IE,如下所示:

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.

我认为确定url的应用程序代码太复杂,无法直接在批处理文件中复制,或者可执行文件的源代码丢失了。如果不是,我个人更愿意在批处理文件本身中显示逻辑。

#2


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

如果返回的字符串包含单行,则可以使用FOR / F设置环境变量的值。例如:

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:\>

#3


start %1 %2

开始%1%2

#4


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.

编辑:Romulo A. Ceccon发布了一个更好的解决方案,它不涉及任何文件系统访问和肮脏的技巧。留在这里作为参考(如果你需要兼容9x,它也适用于command.com),但请更喜欢Romulo的解决方案。


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.)

通过使用从模板动态生成的中间帮助程序脚本来浏览您设置的环境变量。您将需要某处的写权限,否则无法完成(Windows命令shell语言非常非常有限。)

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

让我们使用以下内容调用助手脚本模板helper.tpl:

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.

确保helper.tpl只有一行(没有尾随的CRLF!),并确保在那里等号后没有任何空格。

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

现在,在您的主脚本中,将命令的输出捕获到一个临时文件中(让我们称之为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:

然后将帮助程序模板和输出的内容一起复制到帮助程序脚本中,我们将其称为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:

现在INTERMEDVAR变量被设置为“可执行”输出的第一行(如果它输出多行,你就是你自己......)你现在可以调用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.

当并行多次调用时,这显然不起作用 - 您必须使用当前cmd.exe的PID(例如)来参数化临时文件和帮助程序脚本名称,以便它们不会覆盖彼此的输出,但原则是是一样的。

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

但是,如果你能得到一个真正的shell,那就用它吧。 cmd.exe非常麻烦。

#1


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:

然后你可以将值传递给IE,如下所示:

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.

我认为确定url的应用程序代码太复杂,无法直接在批处理文件中复制,或者可执行文件的源代码丢失了。如果不是,我个人更愿意在批处理文件本身中显示逻辑。

#2


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

如果返回的字符串包含单行,则可以使用FOR / F设置环境变量的值。例如:

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:\>

#3


start %1 %2

开始%1%2

#4


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.

编辑:Romulo A. Ceccon发布了一个更好的解决方案,它不涉及任何文件系统访问和肮脏的技巧。留在这里作为参考(如果你需要兼容9x,它也适用于command.com),但请更喜欢Romulo的解决方案。


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.)

通过使用从模板动态生成的中间帮助程序脚本来浏览您设置的环境变量。您将需要某处的写权限,否则无法完成(Windows命令shell语言非常非常有限。)

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

让我们使用以下内容调用助手脚本模板helper.tpl:

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.

确保helper.tpl只有一行(没有尾随的CRLF!),并确保在那里等号后没有任何空格。

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

现在,在您的主脚本中,将命令的输出捕获到一个临时文件中(让我们称之为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:

然后将帮助程序模板和输出的内容一起复制到帮助程序脚本中,我们将其称为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:

现在INTERMEDVAR变量被设置为“可执行”输出的第一行(如果它输出多行,你就是你自己......)你现在可以调用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.

当并行多次调用时,这显然不起作用 - 您必须使用当前cmd.exe的PID(例如)来参数化临时文件和帮助程序脚本名称,以便它们不会覆盖彼此的输出,但原则是是一样的。

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

但是,如果你能得到一个真正的shell,那就用它吧。 cmd.exe非常麻烦。