Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a log of all ECHO commands in a BATCH file?

I am trying to use a BATCH file as a choose your own adventure story in which the user can create a character and be called by name and such by the characters in the story. It hit me an hour back that it would be cool if as or after the BATCH file containing the story was run it made a log of all of the ECHO commands so that later the player could read what they did on any given play threw.

I would like the log file to read like this:

%date% %time% (all text displayed by echo commands for the length the file runs)

Unfortunately all I can figure out how to do is to make a loge file with just the date and time. Putting ">> StoryLog.txt" works to make the .txt file and I get the date and time in there but it just displays the text ">> StoryLog.txt" after what I want the batch file to display in echoed txt as in "You go north down the path >> StoryLog.txt" is shown on the screen. This naturally just wont work. What do I do?

like image 385
Sean McLain Avatar asked Mar 21 '13 15:03

Sean McLain


People also ask

How do I make a batch file echo?

To create a Windows batch file, follow these steps: Open a text file, such as a Notepad or WordPad document. Add your commands, starting with @echo [off], followed by, each in a new line, title [title of your batch script], echo [first line], and pause. Save your file with the file extension BAT, for example, test.

What is @echo off in batch script?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.


1 Answers

for this purpose I use the following:

set LogFile=somepath\logfile.txt
set logg=^> _^&type _^&type _^>^>%LogFile%
echo this goes to screen AND file! %logg%

This is a bit tricky. So let's disassemble that line to four parts:

set logg=      ^> _          ^&type _           ^&type _^>^>%LogFile%

The Idea is to print the line to a temporary file (named _) (second part) then type the contents of that file to screen (third part) then type it to the logfile (fourth part).

Put that all to a variable (first part), so you don't have to type that monsterstring to every line. (this is the reason why the > and & are escaped with ^)

So every time you use

echo whatever %logg%

it will appear on the screen AND write to %logfile%

Note that this will also work:

 %logg% echo whatever

Edit djangofan: Also, you can do it with functions:

@ECHO off
:: do not enable delayed expansion since it will break this method
SETLOCAL ENABLEEXTENSIONS
SET LogFile=logfile.out
SET Logg=^> tmp.out^&^& type tmp.out^&^&type tmp.out^>^>%LogFile%

CALL :logit "This is my message!"
CALL :logit "Hear my thunder?"

GOTO :end
:logit
ECHO %~1 %Logg%
DEL /Q tmp.out
EXIT /B 0
:end
pause

Edit Stephan: If you use CALL, the %logg% would be overkill. In that case I would just use:

:logit
echo %~1
echo %date%,%time% - %~1 >>logfile
exit /b 0

This might be the best solution to the original question, because the Date/Time will be written into logfile, but not on the screen. Btw: you don't have to delete the tempfile every time you use it, just delete it one time, just before the batch ends.

like image 169
Stephan Avatar answered Oct 14 '22 18:10

Stephan