Batch Redirection and Output

email me

Redirection:

   command > filename        Redirect command output to a file

   command >> filename       APPEND into a file

   command < filename        Type a text file and pass the text to command

   commandA  |  commandB     Pipe the output from commandA into commandB

   commandA &  commandB      Run commandA and then run commandB
   commandA && commandB      Run commandA, if it succeeds then run commandB
   commandA || commandB      Run commandA, if it fails then run commandB

   commandA && commandB || commandC
                             If commandA succeeds run commandB, if it fails commandC

Numeric handles:

STDIN  = 0  Keyboard input
STDOUT = 1  Text output
STDERR = 2  Error text output
UNDEFINED = 3-9

   command 2> filename       Redirect any error message into a file
   command 2>> filename      Append any error message into a file
  (command)2> filename       Redirect any CMD.exe error into a file
   command > file 2>&1       Redirect errors and output to one file
   command > fileA 2> fileB  Redirect output and errors to separate files

   command 2>&1 >filename    This will fail!

Redirect to NUL (hide errors)

   command 2> nul            Redirect error messages to NUL
   command >nul 2>&1         Redirect error and output to NUL
   command >filename 2> nul  Redirect output to file but suppress error
  (command)>filename 2> nul  Redirect output to file but suppress CMD.exe errors

 

Notes

reg Query “HKLM\Hardware\Description\System\CentralProcessor\0” | find /i “x86” > NUL && set OS=32BIT || set OS=64BIT

if %OS%==32BIT goto 32Bit
if %OS%==64BIT goto 64Bit

 

Loading and Running EXEs

@ECHO OFF
FOR /f “tokens=*” %%A IN (‘dir /b *.exe’) DO (
ECHO Filename: “%%A”
::”%%A” /SILENTINSTALLOPTIONS
)
pause