Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for tee.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Keep variables local
  6. SETLOCAL
  7.  
  8. :: Check command line arguments
  9. SET Append=0
  10. IF /I [%1]==[-a] (
  11. 	SET Append=1
  12. 	SHIFT
  13. )
  14. IF     [%1]==[] GOTO Syntax
  15. IF NOT [%2]==[] GOTO Syntax
  16.  
  17. :: Test for invalid wildcards
  18. SET Counter=0
  19. FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
  20. IF %Counter% GTR 1 (
  21. 	SET Counter=
  22. 	GOTO Syntax
  23. )
  24.  
  25. :: A valid filename seems to have been specified
  26. SET File=%1
  27.  
  28. :: Check if a directory with the specified name exists
  29. DIR /AD %File% >NUL 2>NUL
  30. IF NOT ERRORLEVEL 1 (
  31. 	SET File=
  32. 	GOTO Syntax
  33. )
  34.  
  35. :: Specify /Y switch for Windows 2000 / XP COPY command
  36. SET Y=
  37. VER | FIND "Windows NT" > NUL
  38. IF ERRORLEVEL 1 SET Y=/Y
  39.  
  40. :: Flush existing file or create new one if -a wasn't specified
  41. IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)
  42.  
  43. :: Actual TEE
  44. FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
  45. 	>  CON    ECHO.%%B
  46. 	>> %File% ECHO.%%B
  47. )
  48.  
  49. :: Done
  50. ENDLOCAL
  51. GOTO:EOF
  52.  
  53.  
  54. :Count
  55. SET /A Counter += 1
  56. SET File=%1
  57. GOTO:EOF
  58.  
  59.  
  60. :Syntax
  61. ECHO.
  62. ECHO Tee.bat,  Version 2.11a for Windows NT 4 / 2000 / XP
  63. ECHO Display text on screen and redirect it to a file simultaneously
  64. ECHO.
  65. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  some_command  ³  TEE.BAT  [ -a ]  filename
  66. IF NOT "%OS%"=="Windows_NT" GOTO Skip
  67. ECHO Usage:  some_command  ^|  TEE.BAT  [ -a ]  filename
  68. :Skip
  69. ECHO.
  70. ECHO Where:  "some_command" is the command whose output should be redirected
  71. ECHO         "filename"     is the file the output should be redirected to
  72. ECHO         -a             appends the output of the command to the file,
  73. ECHO                        rather than overwriting the file
  74. ECHO.
  75. ECHO Written by Rob van der Woude
  76. ECHO http://www.robvanderwoude.com
  77. ECHO Modified by Kees Couprie
  78. ECHO http://kees.couprie.org
  79. ECHO and Andrew Cameron
  80.  

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