如何在 BATCH 文件中记录所有 ECHO 命令? [英] How do I make a log of all ECHO commands in a BATCH file?

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

问题描述

我正在尝试使用 BATCH 文件作为选择您自己的冒险故事,在该故事中,用户可以创建一个角色并通过故事中的角色的名字等称呼.一个小时前我突然想到,如果在运行包含故事的 BATCH 文件时或之后它会记录所有 ECHO 命令,以便稍后玩家可以阅读他们在任何给定的游戏中所做的事情,那将会很酷.

我希望日志文件如下所示:

%日期%%时间%(echo 命令显示文件运行长度的所有文本)

不幸的是,我只能弄清楚如何做一个只有日期和时间的日志文件.放置>> StoryLog.txt"可以制作.txt文件,我在那里获取日期和时间,但它只显示文本>> StoryLog.txt"之后我希望批处理文件在回显的txt中显示为屏幕上显示你沿着路径向北>> StoryLog.txt".这自然是行不通的.我该怎么办?

解决方案

为此,我使用以下内容:

set LogFile=somepathlogfile.txt设置日志=^>_^&输入_^&输入_^>^>%LogFile%回声这去屏幕和文件!%日志%

这有点棘手.因此,让我们将该行分解为四个部分:

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

想法是将行打印到一个临时文件(名为_)(第二部分)然后键入该文件的内容以进行筛选(第三部分)然后将其输入到日志文件(第四部分)中.

将所有内容都放入一个变量(第一部分)中,这样您就不必在每一行都键入那个怪物字符串.(这就是 >&^ 转义的原因)

所以每次使用

echo %logg%

它将出现在屏幕上并写入%logfile%

请注意,这也适用:

 %logg% 回显任何内容

<块引用>

编辑 djangofan:此外,您可以使用函数来实现:

@ECHO 关闭:: 不要启用延迟扩展,因为它会破坏此方法SETLOCAL 使能扩展SET LogFile=logfile.out设置日志=^>tmp.out^&^&输入 tmp.out^&^&输入 tmp.out^>^>%LogFile%CALL :logit 这是我的信息!"CALL :logit 听到我的雷声了吗?"转到:结束:logit回声 %~1 %Logg%DEL/Q tmp.out退出/B 0:结尾暂停

编辑斯蒂芬:如果您使用 CALL,%logg% 将是矫枉过正.在那种情况下,我只会使用:

:logit回声%~1echo %date%,%time% - %~1 >>日志文件退出/b 0

这可能是原始问题的最佳解决方案,因为日期/时间将写入日志文件,但不会写入屏幕.顺便说一句:您不必每次使用临时文件时都将其删除,只需在批处理结束前删除一次即可.

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?

解决方案

for this purpose I use the following:

set LogFile=somepathlogfile.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.

这篇关于如何在 BATCH 文件中记录所有 ECHO 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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