2013-03-22 2 views
-1

Windows XP 용 배치 파일 내 자신의 답변으로 테이블 결과에 명령의 결과를 재구성하는 방법을 알아야합니다. 예를 들어 나는 명령을 실행하는 경우 :taskkill 결과를 테이블로 재정렬하는 방법

taskkill /f /im iexplore.exe 

을 그리고 그 결과는 다음과 같습니다

성공을 : PID 1553와 과정 "iexplore.exe를"가 종료되었습니다.

나는 어쨌든이 일을 거기에 내 표는이

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

하고 싶어?

+0

예,로 이스케이프 할 필요가에 이 일을하는 방법입니다! 그러나 배치 파일에서 약간의 노력이 필요합니다. Powershell과 같은 것이 태스크에 더 적합 할 것입니다. 길을 따라 가면서 묻는 특정 질문은 그들에게 질문하십시오. 우리는 기꺼이 대답 할 것입니다. –

답변

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 

나는 FOR /F에 대한 소스 '파일'로 ECHO 에드을 유도 할 수있는 변수로 리턴 메시지를 설정했습니다. 실제 사례에서는 따옴표 사이에 taskkill 명령을 사용합니다.

절차 :report을 호출하면 전체 행이 매개 변수로 전달됩니다.

첫 번째 매개 변수는 성공이다 : 당신은 당신이 그것을 사용하실 수 있습니다 방법을 다른 사람에게 같은 단서를 제공하지 않기 때문에하고

네 번째 "iexplore.exe를"나는 단순히 제거, 4 매개 변수 PROCESS을 설정 한 따옴표 및 사용 된 텍스트 RESULT.

다음 단계는 TEXT에 문자열을 첫 번째 매개 변수로 주어진 필드 너비에 중점 지정하고 선택적인 채우기 문자를 두 번째 매개 변수로 가운데에 배치하는 루틴입니다. 이 경우 지정된 길이와 원래 내용이 지정된 채우기 문자 사이에 가운데에 맞춰 TEXT이 반환됩니다.

따라서 (SET text=)&CALL :centre 75 _TEXT을 75 밑줄로 반환합니다. 이것은 uscore75

루틴 :writec1c2은 두 개의 열에 선행 및 후행 파이프와 두 열 사이의 파이프를 써서 씁니다. 열의 두 텍스트 항목은 첫 번째 2 개의 매개 변수로 제공되고 채우기 문자는 세 번째 매개 변수로 제공됩니다. :writec1c2은 모두 두 개의 텍스트 항목을 36 개의 공백 필드에 가운데 놓고 결과 행을 써야합니다.


여기에 약간 포맷 버전입니다 ... 세 밑줄 :writec1c2는 "열 텍스트는"각각의 경우에 밑줄을 의미 호출, 그리고 그들은 밑줄 길이 (36)에 작성하고 있습니다.

그것은 IEXPLORE위한 iexplore 인스턴스를 다음

  • 8 초 대기를 시작한다.exe는
  • taskkill 및 보고서 다시 iexplore을 종료 할
  • 시도 결과 및
  • 가 바닥 글 보고서를 종료 실패를보고 사용 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 

단순히 성공과 failres보고보다 TASKKILL가 표준 오류에 표준 출력 (STDOUT) 및 실패 보고서에 성공 보고서를 보내 주 (STDERR.)이이 STDERR 지시 2>&1 (2 코드에 의해 결합된다) STDOUT (1) 하지만이 이후FOR에 의해 실행 된 명령 내에, 특수 문자 >& 각각 캐럿이 ^

+0

ok 그래도 일괄 처리를 조정하고 재실행하고 내가 생각할 수있는 모든 것을 시도했지만 당신의 설명은 매우 혼란 스러웠고 당신이 원한다면이 일을 할 수 없었습니다. [email protected]으로 전자 메일을 보내주십시오. @PeterWright – cmd

+0

작동 방식이 다릅니다. 전체 아이디어는 다른 사람들이 솔루션을 보거나 개발에 참여할 수 있다는 것입니다. 수정 된 버전을 추가했습니다. 변경 사항, 오류, 수정 방법 등을 명확히하기 위해 몇 가지 설명을 추가하거나 질문을 편집하십시오. – Magoo

+0

ok 고맙습니다. – cmd

관련 문제