windows 在批处理文件中读取标准输入流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6979747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read stdin stream in a batch file
提问by m0tive
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:
我希望能够将一个命令的输出重定向到我的批处理文件process.bat
列表中,因此:
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.
当我用type testFile.txt | process.bat
它测试它时,它会反复打印出第一行。
Is there another way?
还有其他方法吗?
采纳答案by jeb
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.
set /p
不适用于管道,它从输入中取出一行(随机)。
但是您可以more
在 for 循环内部使用。
@echo off
setlocal
for /F "tokens=*" %%a in ('more') do (
echo #%%a
)
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.
但是这在以分号开头的行中失败(因为 eol 的 FOR-LOOP 标准是;
)。
它不能读取空行。
但是使用 findstr 你也可以解决这个问题,它在每一行前面加上行号,所以你永远不会得到空行。
然后前缀被删除到第一个冒号。
@echo off
setlocal DisableDelayedExpansion
for /F "tokens=*" %%a in ('findstr /n $') do (
set "line=%%a"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
echo(!line!
endlocal
)
Alternatively, on some environments (like WinRE) that don't include findstr
, an alternative with find.exe
might suffice. find
will accept a null search string ""
, and allows search inversion. This would allow something like this:
或者,在某些不包含 的环境(如 WinRE)上findstr
,使用 with 的替代方案find.exe
可能就足够了。 find
将接受空搜索字符串""
,并允许搜索反转。这将允许这样的事情:
@echo off
setlocal DisableDelayedExpansion
for /F "tokens=*" %%a in ('find /v ""') do (
...
回答by YumeYao
The set "line=!line:*:=!
" syntax is:
集合 " line=!line:*:=!
" 语法是:
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.
set 需要一个参数,即
a=b
.
如果 a 包含空格或其他内容,则必须在此参数周围使用引号。在这里我没有看到任何!line:*:=!
对于此语法,您可以键入“set /?
”以查看有关使用变量的官方说明。!var!
就像%var%
,获得价值。而是!var!
意味着延迟扩张。
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.
**:= **:=
(空),将变量值中匹配“ *:
”(实际上是从字符串开始到第一次出现:
)的字符串替换为(空),即删除从开始到第一个冒号的子字符串。
回答by Amr Ali
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
> CON ECHO.%%B
>> %File% ECHO.%%B
)
Source here: http://www.robvanderwoude.com/unixports.php#TEE
回答by Dan
Alternatively, on some environments (like WinRE) that don't include findstr
, an alternative with find.exe
might suffice. find
will accept a null search string ""
, and allows search inversion. This would allow something like this:
或者,在某些不包含 的环境(如 WinRE)上findstr
,使用 with 的替代方案find.exe
可能就足够了。 find
将接受空搜索字符串""
,并允许搜索反转。这将允许这样的事情:
@echo off
setlocal DisableDelayedExpansion
for /F "tokens=*" %%a in ('find /v ""') do (
set "line=%%a"
echo(!line!
)