命令行" |在这个时候出人意料。"信息

时间:2016-12-18 06:25:12

标签: command-line

只是想知道为什么我得到" |在这个时候出人意料。"运行以下内容时出现错误消息:

for /f "tokens=2 delims==; " %%a in (' (wmic process call create "%exec%","%workdir%") ^| find "test" ') do set test=%%a

但运行以下内容时没有错误:

 (wmic process call create "%exec%","%workdir%") ^| find "test"

谢谢!

1 个答案:

答案 0 :(得分:0)

解析后的命令中有更多字符escaped

                     (wmic process call create "%exec%","%workdir%") | find "test"
rem  need to escape: ↑                                 ↑           ↑ ↑

下一个命令应该有效 - 尽管在wmic输出中找不到'test'字,可能是:

for /f "tokens=2 delims==; " %%a in (' ^(wmic process call create "%exec%"^,"%workdir%"^) ^| find "test" ') do set test=%%a
rem  properly escaped:                 ↑                                  ↑            ↑  ↑

使用

进行测试
(wmic process call create "cmd.exe","%temp%") | find "ProcessId"

然后,下一个命令将创建的cmd的进程ID设置为变量test

for /f "tokens=2 delims==; " %%a in (' ^(wmic process call create "cmd.exe"^,"%temp%"^) ^| find "ProcessId" ') do set test=%%a