简体   繁体   English

Windows批处理:在由一个称为“批处理”的cmd窗口中运行命令?

[英]Windows Batch: Run command in cmd window opened by a called batch?

I want to run a batch-file A which calls/starts another batch-file B which opens a new cmd-window. 我想运行一个批处理文件A,它调用/启动另一个批处理文件B,这将打开一个新的cmd窗口。 Then I want to run a user-defined command in this new cmd-window. 然后,我想在此新的cmd窗口中运行用户定义的命令。 This command is only recognized after running file B or in this new cmd-window, but the call of the command itself should be written in file A (file B is set and can't be changed). 仅在运行文件B之后或在此新的cmd窗口中才能识别此命令,但是命令本身的调用应写入文件A中(文件B已设置且不能更改)。 How can I do this? 我怎样才能做到这一点?

As far as I understand the behavior, I can't access a cmd-window opened by another batch-file. 据我了解的行为,我无法访问由另一个批处理文件打开的cmd窗口。 I wrote a simple test case to show you the problem, see below. 我编写了一个简单的测试用例来向您展示问题,请参见下文。

I already tried call and start . 我已经尝试callstart If I run batch-file B manually in a cmd-window, I can run the user defined command in the same window because the current cmd window is "updated" and the command is recognized. 如果我在cmd窗口中手动运行批处理文件B,则可以在同一窗口中运行用户定义的命令,因为当前的cmd窗口是“更新的”并且可以识别该命令。

Simple test case: In this example all commands following call scriptB.bat in file A are ignored (probably because the commands are run in the old cmd-window which is "overridden" by the new cmd-window opened in B which is now active). 一个简单的测试用例:在此示例中,文件A中call scriptB.bat之后的所有命令都将被忽略(可能是因为这些命令在旧的cmd窗口中运行,而该命令已被在B中打开的新cmd窗口“覆盖”,该窗口现在处于活动状态) )。 How can I pass the commands, especially the user-defined one, to the new cmd-window? 如何将命令(尤其是用户定义的命令)传递给新的cmd窗口?

scriptA.bat: scriptA.bat:

@echo off

echo Start script!
call scriptB.bat
%user_command%
echo End script!
pause

scriptB.bat: scriptB.bat:

@echo off

set user_command=echo USER COMMAND!
cmd

Output: 输出:

Start script!
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
*my Path*>

After re-reading the Q it became apparent that the user wanted to have variables set in both scripts, and then somehow over-ride the values set in Script B with those in Script A which wee being set after the fact. 重新阅读Q后,很明显,用户希望在两个脚本中都设置变量,然后以某种方式将脚本B中设置的值替换为脚本A中设置的值。

That is not possible without changing Script B to be reading the info for the variable names from the registry using WMIC and having Script A use SetX , or by having Script A write to a file and Script B read from the file. 如果不更改脚本B以使用WMIC从注册表中读取变量名的信息,并让脚本A使用SetX ,或者使脚本A写入文件并从文件中读取脚本B,那么这是不可能的。

However, this woudl create a Race condition as Script A may be setting that values before or after Script B checks. 但是,由于脚本A可能会在脚本B检查之前或之后设置该值,因此这会创建竞赛条件。

Further Script B can not be modified anyway, and if it could then it could just be set up to use a normal SET Command in Script A and then have script B use those variables. 进一步的脚本B无论如何都无法修改,如果可以的话,可以将其设置为在脚本A中使用普通的SET命令,然后让脚本B使用这些变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM