@ECHO OFF
SETLOCAL
SET returntext=SUCCESS: The process "iexplore.exe" with PID 1553 has been terminated.
::
FOR /f "delims=" %%i IN ('echo %returntext%') DO (
CALL :report %%i
)
GOTO :eof
:report
IF %1==SUCCESS: SET process=%~4&SET result=Successfully terminated
:: other translations if required...
(SET text=)&CALL :centre 75 _
SET uscore75=%text%
ECHO %uscore75%
CALL :writec1c2 Process Result
CALL :writec1c2 _ _ _
CALL :writec1c2
CALL :writec1c2 "%process%" "%result%"
CALL :writec1c2
ECHO %uscore75%
GOTO :eof
:writec1c2
SET text=%~1&CALL :centre 36 %3
(SET col1=%text%)
SET text=%~2&CALL :centre 36 %3
(SET col2=%text%)
ECHO ^|%col1%^|%col2%^|
GOTO :eof
:centre
SET fill=%2
IF NOT DEFINED fill (SET fill= )
:centrelp
(SET text=%fill%%text%%fill%)
CALL SET done=%%text:~%1%%
IF NOT DEFINED done GOTO centrelp
CALL SET text=%text:~1%
CALL SET done=%%text:~%1%%
IF DEFINED done SET text=%text:~0,-1%
GOTO :eof
我已将您的返回消息设置为一个变量,该变量被ECHO
编辑为FOR /F
. 在您的实际情况下,您将在引号之间使用 taskkill 命令。
调用过程时:report
,整行作为参数传递。
第一个参数是SUCCESS:第四个是“iexplore.exe”
由于您不知道您可能希望如何使用它,我只是设置PROCESS
为第 4 个参数,去掉引号和RESULT
您使用的文本。
下一步是一个例程,它将字符串集中TEXT
在作为其第一个参数给出的字段宽度和作为其第二个参数的可选填充字符中。这将返回TEXT
指定的长度和以指定填充字符为中心的原始内容。
因此(SET text=)&CALL :centre 75 _
返回TEXT
为 75 个下划线。这存储在uscore75
该例程:writec1c2
用前导和尾随管道以及两列之间的管道写出两列。列的两个文本项作为前 2 个参数提供,填充字符作为第三个参数提供。所需要做:writec1c2
的就是将两个文本项放在一个 36 个空格的字段中,然后写出结果行。
用三个下划线调用:writec1c2
意味着“列文本”在每种情况下都是一个下划线,并且用下划线填充到长度 36...
这是一个稍微重新格式化的版本。
然后它启动一个iexplore
实例
- 等待 8 秒 iexplore.exe 启动
- 以标题开始报告
- 终止
iexplore
使用taskkill
并报告结果
- 尝试
iexplore
再次终止并报告失败
- 以页脚结束报告。
我还添加了更多文档。
在:report
例程中,我包含并跳过了一些代码,这些代码将显示从TASKKILL
. 注意%n
和 %~n` 之间的区别 - 第一个保留引号,第二个删除它们。
@ECHO OFF
SETLOCAL
::
:: Use local routine to make a line of 75 underscores and store it
::
(SET text=)&CALL :centre 75 _
SET uscore75=%text%
::
:: Start iexplore.exe
::
START "Window title here" "C:\Program Files (x86)\Internet Explorer\iexplore.exe" http://www.google.com
::
:: Wait 8 secs for it to start
::
timeout /t 8 >nul
:: Produce the header lines...
ECHO %uscore75%
CALL :writec1c2 Process Result
CALL :writec1c2 _ _ _
CALL :writec1c2
::
:: Now kill iexplore.exe... there will be 2 instances (greedy!)
::
FOR /f "delims=" %%i IN ('taskkill /f /im iexplore.exe 2^>^&1') DO (
CALL :report %%i
)
::
:: Now try again...but she's not there...(Zombies, 1964)
::
FOR /f "delims=" %%i IN ('taskkill /f /im iexplore.exe 2^>^&1') DO (
CALL :report %%i
)
::
:: and the report footer
::
CALL :writec1c2
ECHO %uscore75%
GOTO :eof
:report
::
:: comment-out the following GOTO to show the parameters to the routine
::
GOTO endparms
::
:: By way of explanation, this is what is delivered to the routine...
::
ECHO :report parameters=%*
SET parmno=0
:ploop
SET /a parmno=parmno + 1
IF %parmno% gtr 9 GOTO endparms
CALL SET parmvald=%%~%parmno%%
CALL SET parmval=%%%parmno%%
IF DEFINED parmval (
ECHO parameter %parmno% (%%%parmno%^) to :report = [%parmval%] (%%~%parmno%^) = [%parmvald%]
GOTO ploop)
:endparms
IF %1==SUCCESS: SET process=%~4&SET result=Successfully terminated
IF %1==ERROR: SET process=%~4&SET result=NOT found
:: other translations if required...
CALL :writec1c2 "%process%" "%result%"
GOTO :eof
::
:: strip the quotes from the first two parameters,
:: centre each in a string 36 characters wide.
:: The fill character is given by the third parameter.
:: If no third parameter is supplied, :centre will assume space
::
:: then write PIPE column1 PIPE column2 PIPE
:: The pipe must be escaped by a caret as pipe is a special character
::
:writec1c2
SET text=%~1&CALL :centre 36 %3
(SET col1=%text%)
SET text=%~2&CALL :centre 36 %3
(SET col2=%text%)
ECHO ^|%col1%^|%col2%^|
GOTO :eof
::
:: centre the string in %text%
:: to width %1 using character %2
:: If %2 is not given, use SPACE
::
:centre
:: Set FILL to %2
SET fill=%2
:: If it wasn't provided, set space
IF NOT DEFINED fill (SET fill= )
:centrelp
:: add the fill character to each end of %text%
(SET text=%fill%%text%%fill%)
::
:: Use parsing rule to set DONE to the %1th charater of %text%
:: The parser translates this as
:: CALL (SET done=%text:~[the number supplied in %1]%)
::
CALL SET done=%%text:~%1%%
:: If there was no nth character, not long enough yet,
:: so repeat...
IF NOT DEFINED done GOTO centrelp
:: Now the string is LONGER than required length.
:: Remove the first character, which will be a FILL
:: Then repeat the same parsing trick again to trim off any excess.
CALL SET text=%text:~1%
CALL SET done=%%text:~%1%%
IF DEFINED done SET text=%text:~0,-1%
GOTO :eof
请注意,不是简单地报告成功和失败,而是TASKKILL
将成功报告发送到标准输出 ( STDOUT
) 并将失败报告发送到标准错误 ( STDERR
。) 这些由2>&1
将STDERR
(2) 定向到STDOUT
(1) BUT的代码组合在一起,因为这是在命令中由FOR
, 每个特殊字符执行>
,&
需要用插入符号转义^