在命令行中将 TextLog 添加到 Echo 时,输入不会显示 [英] When adding a TextLog to Echo in Command Line, Input wont show up
问题描述
我知道当我们在 echo
之后添加 >>
时,输出有效,但在命令行本身中它是隐藏的.我想知道是否有一种方法可以在不复制代码的情况下做到这两点.所以它应该显示在命令行和输出文件中.
Echo ### 备份驱动程序 >>%Drive%:BackupBackup-Log.txt平 127.0.0.1 >nulxcopy "%HOMEDRIVE%drivers"/c/d/h/e/i/y "%Drive%:BackupDrivers" >>%Drive%:BackupBackup-Log.txtecho ### 正在备份注册表... >>%Drive%:BackupBackup-Log.txt如果不存在 "%Drive%Registry" mkdir "%Drive%:BackupRegistry"如果存在 "%Drive%Registry
egbackup.reg" Echo "Replaceing %Drive%:BackupRegistry
egbackup.reg" >>%Drive%:BackupBackup-Log.txt如果存在 "%Drive%Registry
egbackup.reg" del "%Drive%:BackupRegistry
egbackup.reg" >>%Drive%:BackupBackup-Log.txtregedit/c/d/h/e/i/y "%Drive%:BackupRegistry
egbackup.reg" >>%Drive%:BackupBackup-Log.txt
这是我的一个旧答案的解释
为此,我使用以下内容:
set LogFile=somepathlogfile.txt设置日志=^>_^&^&输入_^&^&输入_^>^>%LogFile%回声这去屏幕和文件!%日志%
这有点棘手.因此,让我们将该行分解为四个部分:
set logg= ^>_ ^&^&输入_^&^&输入_^>^>%LogFile%
想法是将行打印到临时文件(名为_")(第二部分)然后将该文件的内容输入到屏幕(第三部分)然后将其输入到日志文件(第四部分).>
将所有内容都放入一个变量(第一部分)中,这样您就不必在每一行都键入那个怪物字符串.(这就是>"和&"被^"转义的原因)
所以每次使用
echo %logg%
它将出现在屏幕上并写入 %logfile%
<小时>您可以在这里找到完整的答案:如何在 BATCH 文件中记录所有 ECHO 命令?
注意:
一个&
就够了,所以只写^&
^&^&
缺点是:每次使用都会产生Disk-IO,你使用它.
I know when we add the >>
after echo
the output works but in the command line itself it's hidden. I'd like to know if there is a way I can do both without duplicating my code. So it should show in the command line and the output file.
Echo ### Backing up Drivers >> %Drive%:BackupBackup-Log.txt
Ping 127.0.0.1 >nul
xcopy "%HOMEDRIVE%drivers" /c /d /h /e /i /y "%Drive%:BackupDrivers" >> %Drive%:BackupBackup-Log.txt
echo ### Backing up the Registry... >> %Drive%:BackupBackup-Log.txt
if not exist "%Drive%Registry" mkdir "%Drive%:BackupRegistry"
if exist "%Drive%Registry
egbackup.reg" Echo "Replacing %Drive%:BackupRegistry
egbackup.reg" >> %Drive%:BackupBackup-Log.txt
if exist "%Drive%Registry
egbackup.reg" del "%Drive%:BackupRegistry
egbackup.reg" >> %Drive%:BackupBackup-Log.txt
regedit /c /d /h /e /i /y "%Drive%:BackupRegistry
egbackup.reg" >> %Drive%:BackupBackup-Log.txt
Here is an explanation from an old answer of mine
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%
You can find the complete answer here: How do I make a log of all ECHO commands in a BATCH file?
NOTES:
one &
is enough, so instead of ^&^&
write only ^&
The disadvantage is: it generates Disk-IO every time, you use it.
这篇关于在命令行中将 TextLog 添加到 Echo 时,输入不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!