读取批处理文件中的标准输入流

发布于 2024-11-28 13:13:13 字数 411 浏览 0 评论 0原文

是否可以在批处理文件中使用管道标准输入流?

我希望能够将一个命令的输出重定向到我的批处理文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想你只要分分秒秒 2024-12-05 13:13:14

set /p 不适用于管道,它从输入中获取一行(随机)。
但是您可以在 for 循环内使用 more

@echo off
setlocal
for /F "tokens=*" %%a in ('more') do (
  echo #%%a
)

但对于以分号开头的行会失败(因为 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
)

或者,在某些不包含 findstr 的环境(例如 WinRE)上,使用 find.exe 替代方案可能就足够了。 find 将接受空搜索字符串 "",并允许搜索反转。这将允许这样的事情:

@echo off
setlocal DisableDelayedExpansion

for /F "tokens=*" %%a in ('find /v ""') do (
  ...

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.

@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.

@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:

@echo off
setlocal DisableDelayedExpansion

for /F "tokens=*" %%a in ('find /v ""') do (
  ...
我也只是我 2024-12-05 13:13:14

set“line=!line:*:=!”语法为:

  1. set 需要一个参数,即 a=b
    如果 a 包含空格或其他内容,则必须使用该参数引起来的引号。在这里,我没有看到任何

  2. !line:*:=!
    对于此语法,您可以输入“set /?”来查看有关使用变量的官方说明。
    !var! 类似于 %var%,用于获取值。但 !var! 表示延迟扩展

行 var name

第一

:变量修饰标记。 **:= **:=(空),替换变量值中匹配的字符串“*:”(实际上从字符串开始到第一个 发生)与(空),即删除从开始到第一个冒号的子字符串。

The set "line=!line:*:=!" syntax is:

  1. 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

  2. !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.

画尸师 2024-12-05 13:13:14
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
    >  CON    ECHO.%%B
    >> %File% ECHO.%%B
)

来源:http://www.robvanderwoude.com/unixports.php#TEE

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

笑梦风尘 2024-12-05 13:13:14

或者,在某些不包含 findstr 的环境(例如 WinRE)上,使用 find.exe 替代方案可能就足够了。 find 将接受空搜索字符串 "",并允许搜索反转。这将允许这样的事情:

@echo off
setlocal DisableDelayedExpansion

for /F "tokens=*" %%a in ('find /v ""') do (
  set "line=%%a"
  echo(!line!
)

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:

@echo off
setlocal DisableDelayedExpansion

for /F "tokens=*" %%a in ('find /v ""') do (
  set "line=%%a"
  echo(!line!
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文