我有一个批处理脚本,它将提交证书请求,它将在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
)
答案 0 :(得分: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
下面的批处理代码已写入存储在与GetNumbers.bat
相同的目录中的文件Input.txt
。
@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.txt
和GetNumbers.bat
的目录,方法是输入批处理文件名并按 RETURN 那里没有显示错误,批处理文件使用两行创建Output.txt
:
1892
1893
完成编码任务。
FOR 命令处理文件Input.txt
中的每一行不为空或以分号开头。
它使用普通空格和水平制表符作为字符串分隔符将每一行拆分为子字符串(标记)。
第一个空格/制表符分隔的字符串被分配给循环变量I
,并且由于tokens=1,2
,第二个被分配给循环变量J
,它是{{1}之后的下一个字符} ASCII table。
当至少有一个令牌可以分配给第一个循环变量I
时,执行 FOR 的正文命令块中的命令。
正文命令块中的 IF 条件将区分大小写的第一个空格/制表符分隔字符串与当前行I
进行比较,以验证此行是否包含感兴趣的数字。
如果 IF 条件为真,则批处理代码需要设置第二个循环变量RequestId:
,并且只有感兴趣的数量或双引号中感兴趣的数量
因此进行了比较,将循环变量J
的值与循环变量J
的值进行比较,而不包含双引号。在使用 IF 命令比较两个字符串时,也会考虑周围的双引号。因此,只有当J
具有不包含双引号的数字时才进行此比较。
不包含双引号的数字会附加到文件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
也可以替换为:
Input.txt
请注意,如果字符串应解释为要处理的行的文件名或在后台单独的命令进程中执行的命令行,则周围的单引号会对 FOR 产生影响。从句柄 STDOUT 中捕获哪个输出并逐行处理。
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
'command line which outputs the text to process'
del /?
echo /?
endlocal /?
for /?
if /?
set /?
另请阅读Microsoft有关Using Command Redirection Operators。
的文章