2013-03-22 33 views
-1

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

taskkill /f /im iexplore.exe 

,其结果是:

成功:进程 “IEXPLORE.EXE” 与PID 1553已被终止。

我希望我的表做此

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

反正有这样做的?

+0

是的,有被转义是这样做的一种方式!但是在批处理文件中需要付出一些努力。你可能会发现像Powershell更适合这项任务。如果您有任何具体问题,请询问他们,我们很乐意回答。 – 2013-03-22 22:33:41

回答

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被调用时,整行被作为参数传递。

第一个参数是成功:第四“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执行的命令内,每个特殊字符>&的需要由一个脱字符号^

+0

好吧,所以我尝试调整批次和重做以及我能想到的所有内容,但是您的解释非常混乱,如果您愿意,我无法将其解决。请发送电子邮件至[email protected] @PeterWright – cmd 2013-03-24 00:25:51

+0

它的工作原理并不完全 - 整个想法是其他人可能会看到解决方案或参与其开发。我添加了一个可能有所帮助的修订版本 - 只需添加一些评论(或编辑您的问题)以澄清您的更改,获取的错误,需要修改的内容或其他内容。 – Magoo 2013-03-24 10:37:50

+0

好的,谢谢你这是更有帮助! – cmd 2013-03-24 14:18:00