Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readline.bat

(view source code of readline.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO SyntaxDOS
  4.  
  5. :: Localize variables
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check command line arguments
  9. IF NOT "%~3"=="" GOTO Syntax
  10. IF NOT "%~2"=="" IF /I NOT "%~1"=="/C" GOTO Syntax
  11. IF NOT "%~1"=="" (
  12. 	ECHO.%~1| FINDSTR /R /B /I /C:"/[ACFL]$" >NUL
  13. 	IF ERRORLEVEL 1 GOTO Syntax
  14. )
  15.  
  16. :: Reset variables
  17. SET ReadLine=
  18. SET All=0
  19. SET Count=0
  20. SET First=0
  21. SET Last=0
  22.  
  23. :: Interpret command line
  24. IF    "%~1"==""   SET First=1
  25. IF /I "%~1"=="/A" SET All=1
  26. IF /I "%~1"=="/C" SET /A  Count = %~2
  27. IF /I "%~1"=="/F" SET First=1
  28. IF /I "%~1"=="/L" SET Last=1
  29.  
  30. :: Concatenate all lines
  31. IF %All% EQU 1 (
  32. 	FOR /F "tokens=*" %%A IN ('MORE') DO (
  33. 		SET ReadLine=!ReadLine!%%A
  34. 	)
  35. )
  36.  
  37. :: Show only the first line
  38. IF %First% EQU 1 (
  39. 	FOR /F "tokens=*" %%A IN ('MORE') DO (
  40. 		IF "!ReadLine!"=="" (
  41. 			SET ReadLine=%%A
  42. 		)
  43. 	)
  44. )
  45.  
  46. :: Show only the last line
  47. IF %Last% EQU 1 (
  48. 	FOR /F "tokens=*" %%A IN ('MORE') DO (
  49. 		SET ReadLine=%%A
  50. 	)
  51. )
  52.  
  53. :: Show only the nth line, where n is specified on the command line
  54. IF %Count% GTR 0 (
  55. 	FOR /F "tokens=*" %%A IN ('MORE') DO (
  56. 		SET /A Count -= 1
  57. 		IF !Count! EQU 0 SET ReadLine=%%A
  58. 	)
  59. )
  60.  
  61. SET ReadLine
  62.  
  63. ENDLOCAL & SET ReadLine=%ReadLine%
  64. GOTO:EOF
  65.  
  66.  
  67. :Syntax
  68. ECHO.
  69. ECHO ReadLine.bat,  Version 1.00 for Windows 2000 and later
  70. ECHO Read the specified line^(s^) from the standard input and
  71. ECHO store it ^(or them^) in an environment variable ReadLine
  72. ECHO.
  73. ECHO Usage:  some_command  ^|  READLINE  [ /A ^| /C n ^| /F ^| /L ]
  74. ECHO.
  75. ECHO Where:  some_command  is a command whose standard output is
  76. ECHO                       piped to this script's standard input
  77. ECHO         /A            concatenates all lines
  78. ECHO         /C n          stores only the  Nth   non-empty line
  79. ECHO         /F            stores only the  first non-empty line ^(default^)
  80. ECHO         /L            stores only the  last  non-empty line
  81. ECHO.
  82. ECHO Notes:  Empty lines in some_command's output are skipped.
  83. ECHO         The length of the environment variable is limited.
  84. ECHO         When nothing is piped to this script's standard input,
  85. ECHO         the keyboard is read until the stream is closed by
  86. ECHO         pressing either Ctrl+Z and Enter or F6 and Enter.
  87. ECHO.
  88. ECHO Written by Rob van der Woude
  89. ECHO http://www.robvanderwoude.com
  90.  
  91. :: Exit with ErrorLevel 1
  92. EXIT /B 1
  93.  
  94.  
  95. :: Separate help screens for CMD.EXE and COMMAND.COM because of the pipe symbols used
  96. :SyntaxDOS
  97. ECHO.
  98. ECHO ReadLine.bat,  Version 1.00 for Windows 2000 and later
  99. ECHO Read the specified line(s) from the standard input and
  100. ECHO store it (or them) in an environment variable ReadLine
  101. ECHO.
  102. ECHO Usage:  some_command    READLINE  [ /A  /C n  /F  /L ]
  103. ECHO.
  104. ECHO Where:  some_command  is a command whose standard output is
  105. ECHO                       piped to this script's standard input
  106. ECHO         /A            concatenates all lines
  107. ECHO         /C n          stores only the  Nth   non-empty line
  108. ECHO         /F            stores only the  first non-empty line (default)
  109. ECHO         /L            stores only the  last  non-empty line
  110. ECHO.
  111. ECHO Notes:  Empty lines in some_command's output are skipped.
  112. ECHO         The length of the environment variable is limited.
  113. ECHO         When nothing is piped to this script's standard input,
  114. ECHO         the keyboard is read until the stream is closed by
  115. ECHO         pressing either Ctrl+Z and Enter or F6 and Enter.
  116. ECHO.
  117. ECHO Written by Rob van der Woude
  118. ECHO http://www.robvanderwoude.com
  119.  
  120. :End
  121. :: Exit with ErrorLevel 1
  122. ECHO A | FIND "B"
  123.  

page last uploaded: 2017-04-06, 13:33