windows 如何在 BATCH 文件中记录所有 ECHO 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15551379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:22:37  来源:igfitidea点击:

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

windowsbatch-filecommand-prompt

提问by Sean McLain

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.

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

I would like the log file to read like this:

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

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

%date% %time%(echo 命令显示的文件运行长度的所有文本)

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?

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

回答by Stephan

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%

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

Note that this will also work:

请注意,这也将起作用:

 %logg% echo whatever

Editdjangofan: 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

编辑djangofan:另外,你可以用函数来做:

@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

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

编辑Stephan:如果您使用 CALL,那%logg%将是矫枉过正。在这种情况下,我只会使用:

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

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

回答by Stephan

As my other answer got quite long due to some edits, here is a new suggestion (not much changed, but it makes the code quite easy to read):

由于一些编辑,我的另一个答案很长,这里有一个新建议(没有太大变化,但它使代码很容易阅读):

@ECHO off
SET LogFile=logfile.out
set "say=call :logit "

%say% "This is my message!"
%say% "Hear my thunder?"

GOTO :end
:logit
ECHO %~1 
echo %date% %time% - %~1 >>%logfile%
EXIT /B 0
:end

回答by djangofan

You cannot do that in batch script literally, but if you define functions, you can output data within the function block that also writes to a log file before the function returns, like so:

您不能在批处理脚本中逐字执行此操作,但是如果您定义函数,则可以在函数块中输出数据,该函数块也会在函数返回之前写入日志文件,如下所示:

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
echo. 2>outfile.log

CALL :functionPerson "Jon Doe" jump
GOTO :END
:functionPerson fullname action
  ECHO fullname is ^"%~1^"
  ECHO action is %2
  ECHO %1 performs action %2>> outfile.log
EXIT /B 0
:END
pause

回答by callisto

Each time you echofor the player to know what happens, you could also echointo your log file, adding date and time at the beginning of your line :) Simple as that for me. Don't know how your script looks though.

每次你echo让玩家知道发生了什么时,你也可以echo进入你的日志文件,在你的行的开头添加日期和时间:) 对我来说很简单。不知道你的脚本看起来如何。