2017-05-26 229 views
0

我有一個批處理腳本將提交證書請求,它將在CMD中返回多個RequestId。我希望批處理文件將所有數字提取出來,並將其存儲在一個文件行中,並使用名爲RequestID的文件名。從批處理文件輸出的命令行中提取數

這裏執行腳本

C:\OpenSSL\bin>RequestCert.bat 
Generating a 2048 bit RSA private key 
...................................................................+++ 
...........+++ 
writing new private key to 'xxxx' 
----- 
No value provided for Subject Attribute ST, skipped 
RequestId: 1892 
RequestId: "1892" 
Certificate request is pending: Taken Under Submission (0) 
Generating certificate request and key for xxxx 
ECHO is off. 
------------------------------------------------------------------------ 
Generating a 2048 bit RSA private key 
........+++ 
........................................................................... 
..........+++ 
writing new private key to 'xxxx' 
----- 
No value provided for Subject Attribute ST, skipped 
RequestId: 1893 
RequestId: "1893" 
Certificate request is pending: Taken Under Submission (0) 
Generating certificate request and key for xxxx 
------------------------------------------------------------------------ 
Please approve the certificates before pressing enter. 
Please approve the certificates before pressing enter. 
Please approve the certificates before pressing enter. 
Press any key to continue . . . 

因此,代碼應的requestId後提取出的號碼後是命令行的輸出:

RequestId: 1892 
RequestId: 1893 

的文件應該有:

1892 
1893 

然後,它會取出文件的第一個數字和最後一個數字並將其回顯出來。 例如,請批准從請求ID 900證書920

這是我已經試過:

for /f "tokens=2 delims=:" %%b in ('"C:\program Files\command.exe"'|find 
"RequestId : ") do (
echo %%b >> RequestID.txt 
) 

回答

1

爲了測試批號下面我創建的文件Input.txt與線:

No value provided for Subject Attribute ST, skipped 
RequestId: 1892 
RequestId: "1892" 
Certificate request is pending: Taken Under Submission (0) 
Generating certificate request and key for xxxx 
No value provided for Subject Attribute ST, skipped 
RequestId: 1893 
RequestId: "1893" 
Certificate request is pending: Taken Under Submission (0) 
Generating certificate request and key for xxxx 

下面的批處理代碼被寫入存儲在與Input.txt相同的目錄中的文件GetNumbers.bat

@echo off 
del Output.txt 2>nul 
for /F "tokens=1,2" %%I in (Input.txt) do if "%%I" == "RequestId:" if %%J == %%~J echo %%J>>Output.txt 

在從與當前目錄是與Input.txtGetNumbers.bat目錄通過鍵入批處理文件名和擊打鍵RETURN命令提示窗口內的批處理文件的執行沒有錯誤顯示,並且該批處理文件創建Output.txt兩條線:

1892 
1893 

因此編碼任務完成。

FOR命令處理文件Input.txt中的每行不是空的或以分號開頭。

它使用普通空間和水平選項卡作爲字符串分隔符將每行分割爲子字符串(標記)。

第一空間/製表符分隔的字符串被分配給循環變量I和由於tokens=1,2第二之一被分配給循環變量J是在ASCII tableI之後的下一個字符。

當主體命令塊FOR中的命令在至少1個令牌可被分配給第一循環變量I時被執行。

IF條件在身體命令塊從當前行的第一空間/製表符分隔的字符串進行比較,區分大小寫與RequestId:以驗證是否該行包含感興趣的數目。

如果這IF條件爲真,批處理代碼期望第二個循環變量J也設置,並且只有感興趣的數字或雙引號中的興趣數。

因此,比較循環變量J的值與循環變量J的值,而不包圍雙引號。在將兩個字符串與IF命令進行比較時,也會考慮周圍的雙引號。因此,如果J的數字不包含雙引號,則此比較僅適用於此情況。

不包圍雙引號的數字附加到文件Output.txt

慢一點,但更安全將下面的代碼:

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 
set "DoubleQuote="" 
del Output.txt 2>nul 
for /F "tokens=1,2" %%I in (Input.txt) do (
    if "%%I" == "RequestId:" if "%%~J" NEQ "" (
     set "Number=%%J" 
     if "!Number:~0,1!" NEQ "!DoubleQuote!" echo !Number!>>Output.txt 
    ) 
) 
endlocal 

隨着這批代碼迴路變量J也可以沒有分配的字符串或在其第一批代碼將有語法錯誤而退出的字符串。

'command line which outputs the text to process' 

請注意,周圍的直單引號做出爲區別,如果該字符串應該被解釋爲文件名:

在這兩個批次的代碼塊中的文件名Input.txt可以通過還更換其中要處理的行或命令行要在單獨的命令進程中執行,並在後臺執行輸出以從句柄STDOUT中捕獲並逐行處理。

爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?

請閱讀微軟有關Using Command Redirection Operators的文章。

+0

感謝您的幫助。真的是一個很長的帖子,很多消化哈哈。 – Kaycee

相關問題