Dump batch script output into a text file without specifing batchfile location beforehand
Im aware that i can get the output this way (to capture both standard and errors):
[batchFilePath] > [logFilePath] 2>&1Or this way for standard output:
C:\MyBatchFile.bat > output.txtBut is there a way to do this without specifying the location of the batch file itself?
For example lets assume i run (just doubleclick to execute) C:\MyFolder\MyBatch.cmd and after i exit the batch file (or it completes) i can find the output in C:\MyFolder\MyBatch.txt
If so what code would i need to include in the batch file itself?
2 Answers
The only solution I can think of is adding >> output.txt at the end of every command outputing something of your batch file. It will add the output of your file at the end of output.txt
Or creating a second batch file which content would be .\MyBatchFile.bat > output.txt.
Enclose your commands in round brackets. MyBatchFile.bat:
@echo off
REM setlocal enabledelayedexpansion
( echo line one echo line two echo line three
) > "%~dpn0.txt"Variables inside the brackets are evaluated all at once when ( is encountered. Use enabledelayedexpansion to delay the evaluation.