(view source code of readline.bat as plain text)
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO SyntaxDOS
:: Localize variables
SETLOCAL ENABLEDELAYEDEXPANSION
:: Check command line arguments
IF NOT "%~3"=="" GOTO Syntax
IF NOT "%~2"=="" IF /I NOT "%~1"=="/C" GOTO Syntax
IF NOT "%~1"=="" (
ECHO.%~1| FINDSTR /R /B /I /C:"/[ACFL]$" >NUL
IF ERRORLEVEL 1 GOTO Syntax
)
:: Reset variables
SET ReadLine=
SET All=0
SET Count=0
SET First=0
SET Last=0
:: Interpret command line
IF "%~1"=="" SET First=1
IF /I "%~1"=="/A" SET All=1
IF /I "%~1"=="/C" SET /A Count = %~2
IF /I "%~1"=="/F" SET First=1
IF /I "%~1"=="/L" SET Last=1
:: Concatenate all lines
IF %All% EQU 1 (
FOR /F "tokens=*" %%A IN ('MORE') DO (
SET ReadLine=!ReadLine!%%A
)
)
:: Show only the first line
IF %First% EQU 1 (
FOR /F "tokens=*" %%A IN ('MORE') DO (
IF "!ReadLine!"=="" (
SET ReadLine=%%A
)
)
)
:: Show only the last line
IF %Last% EQU 1 (
FOR /F "tokens=*" %%A IN ('MORE') DO (
SET ReadLine=%%A
)
)
:: Show only the nth line, where n is specified on the command line
IF %Count% GTR 0 (
FOR /F "tokens=*" %%A IN ('MORE') DO (
SET /A Count -= 1
IF !Count! EQU 0 SET ReadLine=%%A
)
)
SET ReadLine
ENDLOCAL & SET ReadLine=%ReadLine%
GOTO:EOF
:Syntax
ECHO.
ECHO ReadLine.bat, Version 1.00 for Windows 2000 and later
ECHO Read the specified line^(s^) from the standard input and
ECHO store it ^(or them^) in an environment variable ReadLine
ECHO.
ECHO Usage: some_command ^| READLINE [ /A ^| /C n ^| /F ^| /L ]
ECHO.
ECHO Where: some_command is a command whose standard output is
ECHO piped to this script's standard input
ECHO /A concatenates all lines
ECHO /C n stores only the Nth non-empty line
ECHO /F stores only the first non-empty line ^(default^)
ECHO /L stores only the last non-empty line
ECHO.
ECHO Notes: Empty lines in some_command's output are skipped.
ECHO The length of the environment variable is limited.
ECHO When nothing is piped to this script's standard input,
ECHO the keyboard is read until the stream is closed by
ECHO pressing either Ctrl+Z and Enter or F6 and Enter.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
:: Exit with ErrorLevel 1
EXIT /B 1
:: Separate help screens for CMD.EXE and COMMAND.COM because of the pipe symbols used
:SyntaxDOS
ECHO.
ECHO ReadLine.bat, Version 1.00 for Windows 2000 and later
ECHO Read the specified line(s) from the standard input and
ECHO store it (or them) in an environment variable ReadLine
ECHO.
ECHO Usage: some_command ³ READLINE [ /A ³ /C n ³ /F ³ /L ]
ECHO.
ECHO Where: some_command is a command whose standard output is
ECHO piped to this script's standard input
ECHO /A concatenates all lines
ECHO /C n stores only the Nth non-empty line
ECHO /F stores only the first non-empty line (default)
ECHO /L stores only the last non-empty line
ECHO.
ECHO Notes: Empty lines in some_command's output are skipped.
ECHO The length of the environment variable is limited.
ECHO When nothing is piped to this script's standard input,
ECHO the keyboard is read until the stream is closed by
ECHO pressing either Ctrl+Z and Enter or F6 and Enter.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
:End
:: Exit with ErrorLevel 1
ECHO A | FIND "B"
page last modified: 2024-04-16; loaded in 0.0060 seconds