Read stdin stream in a batch file

前端 未结 4 710
春和景丽
春和景丽 2020-11-27 17:05

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 l

相关标签:
4条回答
  • 2020-11-27 17:28

    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 (
      ...
    
    0 讨论(0)
  • 2020-11-27 17:40
    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

    0 讨论(0)
  • 2020-11-27 17:46

    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.

    0 讨论(0)
  • 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!
    )
    
    0 讨论(0)
提交回复
热议问题