Celeb Glow
news | February 28, 2026

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>&1

Or this way for standard output:

C:\MyBatchFile.bat > output.txt

But 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.

0

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy