我需要知道如何在windows xp pro的批处理文件中将命令的结果重新组织到一个表中并使用我自己的答案。例如,如果我运行命令:
taskkill /f /im iexplore.exe
结果是:
成功:带有PID 1553的进程“iexplore.exe”已终止。
我希望我的桌子能够做到这一点
___________________________________________________________________________
| Process | Result |
|___________________________________|_____________________________________|
| | |
| iexplore.exe | Successfully terminated |
| | |
___________________________________________________________________________
有没有这样做?
答案 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
终止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
执行的命令中特殊字符>
和&
需要由插入符^