Excel 为什么命令提示符在两个位置之间切换?

Excel 为什么命令提示符在两个位置之间切换?,excel,batch-file,Excel,Batch File,这是我当前的批处理文件 d: SET PATH=%PATH%;D:\_Work\Automation\Selenium\_php5.6.14; IF "%1"=="reports" GOTO reports IF "%1"=="selenium" GOTO automation IF "%1"=="phpunit" GOTO automation IF "%1"=="auto" GOTO automation IF "%1"=="self" GOTO self :reports cd "_W

这是我当前的批处理文件

d:
SET PATH=%PATH%;D:\_Work\Automation\Selenium\_php5.6.14;

IF "%1"=="reports" GOTO reports
IF "%1"=="selenium" GOTO automation
IF "%1"=="phpunit" GOTO automation
IF "%1"=="auto" GOTO automation
IF "%1"=="self" GOTO self

:reports
cd "_Work\Office\Excel"
start /B call "Stored Procedure File List.xlsx"

cd "..\..\TFS\Riley\Main\ReportsSrv"
call ReportsSrv.sln

cd "..\..\..\..\Other\txt"
call "Used Tables.txt"

cd "..\..\Automation\Selenium\Misc Tasks"
call "getTables.php"
call "_getTables-results.sql"

GOTO end

:automation
cd "_Work\Automation\Selenium\_Selenium Server"
start call selenium
cd ../
GOTO end

:self
goto terminate

:terminate
exit

:end
当我运行命令
startup reports
时,当我尝试在批处理文件运行后使用命令提示符时,有时会得到以下结果

注意:
GOTO end
之后的所有内容都是我的输入,而不是批处理文件。空行使我无法进入

当我关闭打开的excel窗口时,会出现这种情况。我怀疑这与
start/B调用“存储过程文件列表.xlsx”
有关。我之所以写这行代码,是因为只要
调用“存储过程文件列表.xlsx”
批处理文件就会暂停,直到excel关闭


我想知道当我退出excel时,为什么命令提示符会在这两个位置之间切换,因为我认为
start/B
会在后台运行命令,我将首先解释该行为

使用START/B选项执行CALL将在同一控制台窗口中启动新的cmd.exe进程。此新进程与原始cmd.exe进程共享stdin和stdout。关闭Excel时,第二个cmd.exe进程仍在运行

现在有两个进程在争夺控制台输入和输出。输入命令时,只有一个进程将接收该命令

假设父进程收到第一个[Enter]——然后打印出正常提示。第二个[Enter]由第二个进程接收,并打印其提示符(由于当前目录不同而有所不同)

如果您发出EXIT,那么其中一个进程将终止,您将只剩下一个进程的普通控制台

解决此问题的一种方法是使用附加的EXIT命令显式启动新的命令进程

start /b cmd /c ""Stored Procedure File List.xlsx" & exit"
但我认为你可以简单地使用

start "" "Stored Procedure File List.xlsx"
如果要开始的第一个参数被引用,那么它将被解释为窗口标题。因此,您的xlsx文件前面有空引号。

语法

  START "title" [/D path] [options] "command" [parameters]
  • 始终包含一个标题这可以是一个简单的字符串,如“我的脚本”,也可以是一对空引号
  • 根据Microsoft文档,标题是可选的,但是根据选择的其他选项,如果省略它,则可能会出现问题
使用(我不确定
/B
开关):

下一个类似代码的部分展示了如何重现问题(这与此无关)

说明:

  • 前三个命令(
    md
    cd
    type
    ):创建操作环境
  • wmic
    命令显示当前打开的
    cmd
    窗口的特定属性
  • 错误使用
    start
    命令:
    start/B call test.txt
  • wmic
    命令显示已创建子(隐藏)
    cmd
    窗口
  • cd..
    后跟一些输入显示当前目录切换
  • doskey/history
    显示事实上我在那个child
    cmd
    提示符中
  • exit/B
    关闭隐藏的子项
    cmd
    提示符
  • doskey/history
    和下面的
    wmic
    显示我回到了原始
    cmd
    提示符中
  • cmd/K
    会再次将我返回到看似相同的子
    cmd
    提示符(请参见下一步
    doskey/history
    ),但现在正常情况下没有这种奇怪的切换效果

如何:启动/等待?不,它不会在后台运行。不使用
/b启动
将程序单独运行到当前控制台。当与
/b
一起使用时,控制台程序(这是
/b
影响的,因为GUI程序不在控制台中运行)在同一控制台中运行(否定使用
启动的效果)。没有这样的命令可以在控制台的后台运行程序。另外,
Call
用于启动批处理文件。请参见
start/?
call/?
。退出批处理文件的标准方法是
Goto:eof
。请参见
转到/?
。总结@ddbug no。当我在没有
/B
的情况下尝试它时,它与以前使用
调用“存储过程文件列表.xlsx”
时做的一样,只是我也必须退出新的命令提示符窗口。使用
/B
它做的事情与我在问题中描述的一样,当我退出excel窗口时,GUI程序是一个非控制台程序。规则是。一个程序本身在一行中-控制台程序在控制台中启动,并成批等待GUI程序退出(如果键入的程序不等待)。要更改上述行为,请在新窗口中使用
start
-控制台程序启动,原始窗口不再等待,命令不再等待GUI程序
Call
用于启动批处理文件并等待它(默认情况下,控制权转移到批处理,调用批处理结束)。在
start
中,第一组引号是窗口标题。
start”“/B”test.txt”
不会启动另一个
cmd
进程。因此,
/B
开关本身并不是罪魁祸首,但只有在
调用时才是罪魁祸首,即使在
start”“/B CALL“test.txt”
@JosefZ中也是如此-是的,我编辑了我的答案以使之更清楚。
start "" /B "Stored Procedure File List.xlsx"
d:\bat> md \a\b\c\d

d:\bat> cd \a\b\c\d

d:\a\b\c\d> >test.txt type nul

d:\a\b\c\d> wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
CommandLine                    Handle  ParentProcessId  ProcessId
"C:\Windows\system32\cmd.exe"  6588    3284             6588


d:\a\b\c\d> start /B call test.txt

d:\a\b\c\d>
d:\a\b\c\d> wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
CommandLine                                     Handle  ParentProcessId  ProcessId
"C:\Windows\system32\cmd.exe"                   6588    3284             6588
C:\Windows\system32\cmd.exe  /K call  test.txt  5556    6588             5556


d:\a\b\c\d> cd ..

d:\a\b\c>
d:\a\b\c\d>
d:\a\b\c>
d:\a\b\c\d>
d:\a\b\c> doskey /history
doskey /history

d:\a\b\c\d> doskey /history
doskey /history

d:\a\b\c> exit /B

d:\a\b\c> doskey /history
d
cls
md \a\b\c\d
cd \a\b\c\d
>test.txt type nul
wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
start /B call test.txt
wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
cd ..
doskey /history

d:\a\b\c> wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
CommandLine                    Handle  ParentProcessId  ProcessId
"C:\Windows\system32\cmd.exe"  6588    3284             6588


d:\a\b\c> cmd /K
d:\a\b\c> doskey /history
doskey /history
exit /B
doskey /history

d:\a\b\c>
d:\a\b\c>