Enhanced version of Avrumi Sherman with handling of STDERR and STDOUT:
D:\>dir ERROR 1>log.txt 2>&1 & type log.txt
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: 4653-A096
Verzeichnis von D:\Projekte\UDG_TMP\build-deploy
Datei nicht gefunden
So it displays the STDTOUT (first lines) and the STDERR (last line) on screen and the log-file.
For usage in a batch file just do it like Avrumi Sherman did but use a single & in order to catch the ERROR-cases as well:
D:\>set logFile=log.txt
D:\>set tmpLog=tmp.txt
D:\>set logCmd=1^>%tmpLog% 2^>^&1 ^& type %tmpLog% ^& type %tmpLog%^>^>%logFile%
Now you can use the logCmd variable like a logCmd ;)
Note the ERROR messages of the first dir and the second rmdir are displayed on screen as well as appearing in the log-file:
D:\>echo START %logCmd%
START
D:\>dir DIRECTORY %logCmd%
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: 4653-A096
Verzeichnis von D:\
Datei nicht gefunden
D:\>mkdir DIRECTORY %logCmd%
D:\>dir DIRECTORY %logCmd%
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: 4653-A096
Verzeichnis von D:\DIRECTORY
26.08.2020 17:50 <DIR> .
26.08.2020 17:50 <DIR> ..
0 Datei(en), 0 Bytes
2 Verzeichnis(se), 68.391.989.248 Bytes frei
D:\>rmdir DIRECTORY %logCmd%
D:\>rmdir DIRECTORY %logCmd%
Das System kann die angegebene Datei nicht finden.
D:\>echo STOP %logCmd%
STOP
At the end of your batch file you can/should delete %tmpLog%. Here the content of %logFile% using the example above:
START
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: 4653-A096
Verzeichnis von D:\
Datei nicht gefunden
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: 4653-A096
Verzeichnis von D:\DIRECTORY
26.08.2020 17:50 <DIR> .
26.08.2020 17:50 <DIR> ..
0 Datei(en), 0 Bytes
2 Verzeichnis(se), 68.391.989.248 Bytes frei
Das System kann die angegebene Datei nicht finden.
STOP