Advertisement
T3RRYT3RR0R

TEE macro - batch tee

Mar 25th, 2021
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.45 KB | None | 0 0
  1. @Echo off & CD "%~dp0"
  2.  
  3. If "!" == "" (
  4.  Echo(Delayed Expansion not permitted prior to definition of TEE macro
  5.  Pause
  6.  Exit /B 1
  7. )
  8.  
  9. (Set \n=^^^
  10.  
  11. %=  \n Macro newline Variable. Do not modify =%)
  12.  
  13.  Set TEE=For %%n in (1 2)Do if %%n==2 (%\n%
  14.    ^>^>"File" Echo(------------------------------------------------------------------%\n%
  15.    ^>^>"File" Echo( !Date! @ !TIME!%\n%
  16.    ^>^>"File" Echo( !CMDArg!%\n%
  17.    ^>^>"File" Echo(------------------------------------------------------------------%\n%
  18.   For /F "Delims=" %%G in ('@^(!CMDArg!^^)')Do (%\n%
  19.    Echo(%%G%\n%
  20.    ^>^>"File" (Echo(%%G)%\n%
  21.   )%\n%
  22.    ^>^>"File" Echo(%\n%
  23.  )Else Set CMDArg=
  24.  
  25. Setlocal EnableExtensions EnableDelayedExpansion
  26.  
  27. :# Output filename.ext supplied during expansion of TEE macro using Substring modification
  28. :# For multiline commands, the linefeed should be escaped
  29. :#  - The leading character of line following escaped linefeed is consumed by the escape.
  30. :# Echo statements preceeding escaped linefeeds should be parenthesised to ensure no unintended whitespace is output
  31. :# Command tokens < > & | require escaping
  32.  
  33. :# Examples of using TEE with different command types:
  34.  
  35.  Set "input="
  36.  Set /P "input=Enter Input: "
  37.  If defined input (
  38.   %TEE:File=Outfile.dat% Echo(%input%
  39.  )Else %TEE:File=Outfile.dat% Set TEE
  40.  
  41.  %TEE:File=Outfile.dat% For /L %%i in (1 1 3)Do (^
  42.   (Echo(Square %%i)^
  43.   ^& (Echo(%%i*%%i)^
  44.  )
  45.  
  46.  %TEE:File=Outfile.dat% ^<nul Set /P "=Hello world"
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement