Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cut.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Localize variables
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check NUMBER of command line arguments
  9. IF     "%~1"=="" GOTO Syntax
  10. IF NOT "%~4"=="" GOTO Syntax
  11.  
  12. :: Initialize variables
  13. SET Param.C=
  14. SET Param.D=
  15. SET Param.F=
  16. SET Param.L=
  17. SET Error=0
  18.  
  19. :: Parse command line arguments
  20. FOR %%A IN (%*) DO (
  21. 	ECHO.%%A | FINDSTR /R /B /I /C:"-[CDFL]:.*$" >NUL
  22. 	IF NOT ERRORLEVEL 1 (
  23. 		FOR /F "tokens=1* delims=-:" %%B IN ("%%~A") DO (
  24. 			SET Param.%%B=%%~C
  25. 			IF /I "%%~B"=="D" (
  26. 				IF NOT "!Param.D:~0,1!"=="!Param.D!" SET Error=1
  27. 			) ELSE (
  28. 				SET /A ParamTest = Param.%%B
  29. 				IF !Paramtest! EQU   0 SET Error=1
  30. 				IF !ParamTest! NEQ %%C SET Error=1
  31. 			)
  32. 		)
  33. 	)
  34. )
  35. IF %Error% EQU 1 GOTO Syntax
  36.  
  37. :: Either -C or -F must be used, one and only one
  38. IF NOT DEFINED Param.C IF NOT DEFINED Param.F GOTO Syntax
  39. IF     DEFINED Param.C IF     DEFINED Param.F GOTO Syntax
  40.  
  41. :: If -C was specified
  42. IF DEFINED Param.C (
  43. 	SET /A StartPos = Param.C - 1
  44. 	IF DEFINED Param.L (SET Length=,%Param.L%) ELSE (SET Length=)
  45. 	FOR /F "tokens=* delims=" %%A IN ('MORE') DO (
  46. 		SET Line=%%A
  47. 		rem CALL ECHO !Line:~%StartPos%%Length%!
  48. 		CALL :DisplaySubstr "!StartPos!" "!Length!"
  49. 	)
  50. 	ENDLOCAL
  51. 	GOTO:EOF
  52. )
  53.  
  54. :: If not -C then -F must have been specified
  55. IF DEFINED Param.L (
  56. 	SET /A LastPos  = %Param.F% + %Param.L% - 1
  57. ) ELSE (
  58. 	SET /A StartPos = %Param.F% - 1
  59. )
  60. IF DEFINED Param.D (
  61. 	SET Delims= delims=%Param.D%
  62. ) ELSE (
  63. 	SET Delims=
  64. )
  65. IF DEFINED Param.L (
  66. 	FOR /F "tokens=* delims=" %%A IN ('MORE') DO (
  67. 		SET Line=
  68. 		FOR /L %%B IN (%Param.F%,1,%LastPos%) DO (
  69. 			CALL :AddWord "%%~B" "%Delims%" "%%~A"
  70. 		)
  71. 		IF NOT "!Line!"=="" ECHO !Line:~1!
  72. 	)
  73. ) ELSE (
  74. 	FOR /F "tokens=%StartPos%*%Delims%" %%A IN ('MORE') DO (
  75. 		ECHO.%%~B
  76. 	)
  77. )
  78.  
  79. ENDLOCAL
  80. GOTO:EOF
  81.  
  82.  
  83. :AddWord
  84. FOR /F "tokens=%~1%~2" %%C IN ("%~3") DO (SET Line=!Line! %%C)
  85. GOTO:EOF
  86.  
  87.  
  88. :DisplaySubstr
  89. CALL ECHO.!Line:^~%~1%~2!
  90. GOTO:EOF
  91.  
  92.  
  93. :Syntax
  94. ECHO.
  95. ECHO Cut.bat,  Version 1.00 for Windows 2000 and later
  96. ECHO Attempt to "port" the Unix CUT command to batch
  97. ECHO.
  98. :: Separate help for DOS and NT because in DOS pipe symbols cannot be
  99. :: escaped and hence are emulated using extended ASCII character 179
  100. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  command  ³  CUT  -C:n   [ -L:n ]
  101. IF NOT "%OS%"=="Windows_NT" ECHO    or:  command  ³  CUT  -F:n   [ -L:n ]  [ -D:"chr" ]
  102. IF NOT "%OS%"=="Windows_NT" GOTO SkipPipes
  103. ECHO Usage:  command  ^|  CUT  -C:n   [ -L:n ]
  104. ECHO    or:  command  ^|  CUT  -F:n   [ -L:n ]  [ -D:"chr" ]
  105. :SkipPipes
  106. ECHO.
  107. ECHO Where:  command  is a command whose standard output is piped to this script
  108. ECHO         -C:n     cuts the standard input at the Nth "column" (character)
  109. ECHO         -F:n     cuts the standard input at the Nth "field"  (word)
  110. ECHO         -D:"chr" specifies delimiter for -F  (default: space)
  111. ECHO         -L:n     maximum N columns or fields (default: all till end of line)
  112. ECHO.
  113. ECHO Notes:  Empty lines in the standard input as well as in the output are skipped.
  114. ECHO         With -F and -L specified, multiple consecutive delimiters are treated
  115. ECHO         as a single delimiter in the output.
  116. ECHO         Delimiter specified with -D is case insensitive.
  117. ECHO         When nothing is piped to this script's standard input, the keyboard is
  118. ECHO         read until the stream is closed by pressing either Ctrl+Z and Enter or
  119. ECHO         F6 and Enter.
  120. ECHO.
  121. ECHO Written by Rob van der Woude
  122. ECHO http://www.robvanderwoude.com
  123.  
  124. IF "%OS%"=="Windows_NT" ENDLOCAL
  125.  
  126. :: Exit with ErrorLevel 1, even in "true" DOS
  127. ECHO A | FIND "B"
  128.  

page last modified: 2024-04-16; loaded in 0.0087 seconds