如何将taskkill结果重新排序为表

时间:2013-03-22 21:58:01

标签: windows batch-file

我需要知道如何在windows xp pro的批处理文件中将命令的结果重新组织到一个表中并使用我自己的答案。例如,如果我运行命令:

taskkill /f /im iexplore.exe

结果是:

  

成功:带有PID 1553的进程“iexplore.exe”已终止。

我希望我的桌子能够做到这一点

___________________________________________________________________________
|             Process               |              Result                 |
|___________________________________|_____________________________________|
|                                   |                                     |
|         iexplore.exe              |      Successfully terminated        |
|                                   |                                     |
___________________________________________________________________________

有没有这样做?

1 个答案:

答案 0 :(得分:1)

@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设置为您使用的文本。< / p>

下一步是一个例程,它将TEXT中的字符串居中作为第一个参数给定的字段宽度,将可选填充字符作为第二个参数。这将返回TEXT指定的长度,并且原始内容居中于指定的填充字符之间。

因此(SET text=)&CALL :centre 75 _返回TEXT作为75个下划线。这存储在uscore75

例程:writec1c2写出两列,其中包含前导和尾随管道以及两列之间的管道。列的两个文本项作为前两个参数提供,填充字符作为第三个参数。所有:writec1c2需要做的是将两个文本项置于36个空格的字段中,并写出结果行。

使用三个下划线调用:writec1c2意味着“列文本”在每种情况下都是下划线,并且它们用下划线填充到长度为36 ......


这是一个稍微重新格式化的版本。

它启动iexplore实例,然后

  • 等待iexplore.exe启动等待8秒
  • 使用标题
  • 启动报告
  • 使用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执行的命令中特殊字符>&需要由插入符^

转义