我如何在一个批处理文件日志所有ECHO命令? [英] How do I make a log of all ECHO commands in a BATCH file?

查看:483
本文介绍了我如何在一个批处理文件日志所有ECHO命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用批处理文件作为选择你自己的冒险故事中,用户可以创建人物和故事中的人物可以通过姓名和调用。它击中了我一个小时回来,这将是冷静,如果为或包含故事批处理文件后,运行它做了一个日志中的所有ECHO的命令,以便以后播放器可以读取他们做了什么对任意给定的游戏扔。

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%
(由回声命令的文件运行长度显示的所有文本)

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

不幸的是我能想出如何做的是做一个包厢文件,只是日期和时间。把>> StoryLog.txt工程,使.txt文件,我得到的日期和时间在那里,但之后我想该批处理文件中回荡TXT作为显示它只是显示文本​​>> StoryLog.txt在你去北面沿着小路>> StoryLog.txt显示在屏幕上。这自然只是不会工作。我该怎么办?

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?

推荐答案

为此我使用了以下内容:

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

把所有的一个变量(第一部分),所以你不必键入monsterstring到每一行。 (这就是为什么>和&放大器;的原因被转义为^)

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 "^")

所以,每次使用时间

echo whatever %logg%

它就会出现在屏幕上,并写入日志文件%%

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

请注意,这也将工作:

%logg% echo whatever

此外,您还可以用函数做到这一点:

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

编辑史蒂芬:
如果您使用的电话,%LOGG%将是矫枉过正。在这种情况下,我只想用:

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.

这篇关于我如何在一个批处理文件日志所有ECHO命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆