@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
被调用时,整行被作为参数传递。
第一个参数是成功:第四“IEXPLORE.EXE”
既然你没有给出线索,你不妨要不然怎么使用它,我简单地设置PROCESS
第4个参数,剥引用和RESULT
到您使用的文本。
下一步是一个例程,它以TEXT
中的字符串为中心,字段宽度为第一个参数,第二个为可选的填充字符。这将返回指定长度的TEXT
,并且原始内容位于指定的填充字符之间。
因此(SET text=)&CALL :centre 75 _
返回TEXT
为75个下划线。这存储在uscore75
例程:writec1c2
写出两列与一个前导和尾随管加上两列之间的管道。列的两个文本项作为前两个参数提供,填充字符作为第三个参数。所有:writec1c2
需要做的是将两个文本项放在36个空格的字段中并写出结果行。
调用:writec1c2
有三个下划线意味着“列文本”在每种情况下的下划线,而且他们充满与下划线长36 ...
这里有一个稍微重排版本。
它启动一个iexplore
实例然后
- 等待8秒为IEXPLORE。exe文件来启动
- 开始用头
- 终止
iexplore
使用taskkill
和报告结果
- 尝试再次终止
iexplore
和报告故障
- 结束与页脚报告的报告。
我也增加了很多文档。
在:report
例程中,我包含并跳过了一段代码,它将显示从TASKKILL
传递的参数。请注意0和%〜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
注意,而不是简单地报告成功和failres,TASKKILL
发送成功报告到标准输出(STDOUT
)和故障报告给标准误差(STDERR
。)这些是由代码组合由2>&1
其引导STDERR
(2 )至STDOUT
(1)BUT,因为这是由FOR
执行的命令内,每个特殊字符>
和&
的需要由一个脱字符号^
是的,有被转义是这样做的一种方式!但是在批处理文件中需要付出一些努力。你可能会发现像Powershell更适合这项任务。如果您有任何具体问题,请询问他们,我们很乐意回答。 – 2013-03-22 22:33:41