Dump batch script output into a text file without specifing batchfile location beforehand

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.


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.