读取批处理文件中的标准输入流
是否可以在批处理文件中使用管道标准输入流?
我希望能够将一个命令的输出重定向到我的批处理文件 process.bat
列表中,这样:
C:\>someOtherProgram.exe | process.bat
我的第一次尝试如下:
echo OFF
setlocal
:again
set /p inputLine=""
echo.%inputLine%
if not (%inputLine%)==() goto again
endlocal
:End
当我使用 type testFile.txt | 测试它时process.bat
它会重复打印第一行。
还有别的办法吗?
Is it possible to use a piped stdin stream inside a batch file?
I want to be able to redirect the output of one command into my batch file process.bat
list so:
C:\>someOtherProgram.exe | process.bat
My first attempt looked like:
echo OFF
setlocal
:again
set /p inputLine=""
echo.%inputLine%
if not (%inputLine%)==() goto again
endlocal
:End
When I test it with type testFile.txt | process.bat
it prints out the first line repeatedly.
Is there another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
set /p
不适用于管道,它从输入中获取一行(随机)。但是您可以在 for 循环内使用
more
。但对于以分号开头的行会失败(因为 eol 的 FOR-LOOP 标准是
;
)。并且它无法读取空行。
但是使用 findstr 你也可以解决这个问题,它在每行前面加上行号,所以你永远不会得到空行。
然后前缀被删除到第一个冒号。
或者,在某些不包含
findstr
的环境(例如 WinRE)上,使用find.exe
替代方案可能就足够了。find
将接受空搜索字符串""
,并允许搜索反转。这将允许这样的事情:set /p
doesn't work with pipes, it takes one (randomly) line from the input.But you can use
more
inside of an for-loop.But this fails with lines beginning with a semicolon (as the FOR-LOOP-standard of eol is
;
).And it can't read empty lines.
But with findstr you can solve this too, it prefix each line with the linenumber, so you never get empty lines.
And then the prefix is removed to the first colon.
Alternatively, on some environments (like WinRE) that don't include
findstr
, an alternative withfind.exe
might suffice.find
will accept a null search string""
, and allows search inversion. This would allow something like this:set“
line=!line:*:=!
”语法为:set 需要一个参数,即
a=b
。如果 a 包含空格或其他内容,则必须使用该参数引起来的引号。在这里,我没有看到任何
!line:*:=!
对于此语法,您可以输入“
set /?
”来查看有关使用变量的官方说明。!var!
类似于%var%
,用于获取值。但!var!
表示延迟扩展。行 var name
第一
:变量修饰标记。
**:= **:=
(空),替换变量值中匹配的字符串“*:
”(实际上从字符串开始到第一个:
发生)与(空),即删除从开始到第一个冒号的子字符串。The set "
line=!line:*:=!
" syntax is:set requires one parameter that is
a=b
.If a contains a space or something, you'll have to use the quotation marks around this parameter. Here I don't see any
!line:*:=!
For this syntax, you can type '
set /?
' to see the official description on using variables.!var!
is like%var%
, to get the value. But!var!
means delayed expansion.line var name
the first : variable modification mark.
**:= **:=
(empty), replace the string in the variable's value matches "*:
"(virtually from the string start to first:
occurence) with (empty), i.e. delete the substring from start to first colon.来源:http://www.robvanderwoude.com/unixports.php#TEE
Source here: http://www.robvanderwoude.com/unixports.php#TEE
或者,在某些不包含
findstr
的环境(例如 WinRE)上,使用find.exe
替代方案可能就足够了。find
将接受空搜索字符串""
,并允许搜索反转。这将允许这样的事情:Alternatively, on some environments (like WinRE) that don't include
findstr
, an alternative withfind.exe
might suffice.find
will accept a null search string""
, and allows search inversion. This would allow something like this: