drum
drum

Reputation: 5651

How to write command to file in batch?

In batch, is there a way to write the command to run into a file?

For example my script is:

ping 127.0.0.1 >> file.txt

I want to include ping 127.0.0.1 into my file.txt so I would know which command produced which output.

Of course I could simply do:

echo "ping 127.0.0.1" >> file.txt
ping 127.0.0.1 >> file.txt

Upvotes: 2

Views: 752

Answers (2)

David Ruhmann
David Ruhmann

Reputation: 11367

When calling the batch script, leave echo on and output the entire script.

Command Line

script.bat >> file.txt

Script.bat

@echo on
ping "127.0.0.1"

This can also be self-invoked by the script with

@echo off
if /i not "%~1"=="self" call "%~f0" self >> file.txt & goto :EOF
@echo on
:: Everything (including commands) after this echo will be displayed in the file
ping "127.0.0.1"

Upvotes: 4

Stephan
Stephan

Reputation: 56180

How about

@echo on

before a block of commands, you want to log, and an

@echo off

after that?

@echo off
rem what i want to do
rem some commands (setting variables etc.)
@echo on
ping 127.0.0.1" >> file.txt
ipconfig /all >>file.txt
@echo off
rem some other commands (deleting temporary files etc) 

Upvotes: 0

Related Questions