Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Logging 在批处理文件中添加命令以生成文本日志(通过双击启动)_Logging_Batch File_Cmd - Fatal编程技术网

Logging 在批处理文件中添加命令以生成文本日志(通过双击启动)

Logging 在批处理文件中添加命令以生成文本日志(通过双击启动),logging,batch-file,cmd,Logging,Batch File,Cmd,高级感谢任何前来阅读本文的人。以下是我的设想: 情景 操作系统是w7px64。我以前没有使用过.bat文件,但对cmd有些熟悉。 我用几十个命令创建了一个成功的.bat文件,使用SC、net stop、schtasks、powercfg等命令修改进程、服务、计划任务和电源方案。。 我将始终通过在Windows资源管理器中双击来启动.bat,而不会从CMD本身启动。 该文件以暂停命令结束,以在完成所有任务后保持cmd窗口打开,使我能够滚动查看结果。 该文件只包含上面提到的命令:不使用echo或其他

高级感谢任何前来阅读本文的人。以下是我的设想:

情景

操作系统是w7px64。我以前没有使用过.bat文件,但对cmd有些熟悉。 我用几十个命令创建了一个成功的.bat文件,使用SC、net stop、schtasks、powercfg等命令修改进程、服务、计划任务和电源方案。。 我将始终通过在Windows资源管理器中双击来启动.bat,而不会从CMD本身启动。 该文件以暂停命令结束,以在完成所有任务后保持cmd窗口打开,使我能够滚动查看结果。 该文件只包含上面提到的命令:不使用echo或其他任何东西。我喜欢看到完整的结果,它有助于我的故障排除。 问题

虽然文件和命令可以很好地满足我的需要,但即使在cmd设置中最大化缓冲区后,结果对于cmd来说也太长了;在执行最后一个命令时,我再也不能向上滚动到第一个命令-由于空间限制,cmd已清除它们

目标

我想要一种方法,使上述所有内容都能按原样继续下去,即使我无法在CMD中读取所有内容,但也可以在.bat文件中添加一个命令,将所有结果同时输出到一个文本文件中,就像屏幕上显示的那样:成功、失败、错误-一切

有这样的命令吗

另外,如果有人知道cmd中空间不足的解决方法,我也会很喜欢。再次感谢


<

假设批处理文件不是交互式的,并且在运行时不需要查看它们,那么这将为您提供一个日志:

@echo off
call "d:\path here\to folder\mybatch" >"z:\logs\mybatch.log" 2>&1
start "" notepad "z:\logs\mybatch.log"
运行此批处理文件后,screenOut.txt文件的内容如下:

C:\Documents and Settings\Antonio\My Documents\test
>if defined Restart goto Begin 

C:\Documents and Settings\Antonio\My Documents\test
>rem Place your Batch code here; for example: 

C:\Documents and Settings\Antonio\My Documents\test
>rem The date: 05/06/2013, the time: 14:46:54.65 

C:\Documents and Settings\Antonio\My Documents\test
>dir TeeMyself.* 
 Volume in drive C has no label.
 Volume Serial Number is CCA1-5338

 Directory of C:\Documents and Settings\Antonio\My Documents\test

05/06/2013  02:43 p.m.               748 TeeMyself.bat
               1 File(s)            748 bytes
               0 Dir(s)  15,004,553,216 bytes free

C:\Documents and Settings\Antonio\My Documents\test
>rem An error message sent to STDERR 

C:\Documents and Settings\Antonio\My Documents\test
>verify bad 
An incorrect parameter was
entered for the command.

C:\Documents and Settings\Antonio\My Documents\test
>goto :EOF 
屏幕内容如下:

C:\Documents and Settings\Antonio\My Documents\test
>if defined Restart goto Begin

C:\Documents and Settings\Antonio\My Documents\test
>rem TeeMyself.bat: Send output of this Batch file into screenOut.txt file

C:\Documents and Settings\Antonio\My Documents\test
>rem Antonio Perez Ayala

C:\Documents and Settings\Antonio\My Documents\test
>set Restart=True

C:\Documents and Settings\Antonio\My Documents\test
>call "C:\Documents and Settings\Antonio\My Documents\test\TeeMyself.bat"   2>&1  | Cscript //nologo //E:JScript "C:\Documents and Settings\Antonio\My Documents
\test\TeeMyself.bat"  2>screenOut.txt

===================================================
 The same contents of screenOut.txt file goes here
===================================================

C:\Documents and Settings\Antonio\My Documents\test
>set Restart=

C:\Documents and Settings\Antonio\My Documents\test
>goto :EOF

要在批处理文件的同一文件夹中创建文本文件,应使用%DP0screenOut.txt名称。

这样的命令不存在。您必须使用tee、GNUWin或批处理版本。或者切换到具有tee cmdlet的PowerShell。使用某些版本的tee不是Windows CMD提示符的本机版本,但适用于Windows是一个不错的选择。但是,如果您只需要看到从窗口顶部滚动的内容,只需编辑窗口属性即可。在窗口标题栏上单击鼠标右键,选择“属性”,然后选择“布局”选项卡。将缓冲区宽度和高度设置为比窗口大小大得多的值。我经常将我的宽度设置为1000,高度设置为3000,然后我可以滚动查看几乎所有我需要的东西。显然,必须在执行命令之前进行更改。@dbenham:很好的建议,Dave,但是OP通过在资源管理器中双击来执行批处理文件!我曾经创建MS-DOS图标的快捷方式并修改其属性,但我不知道如何修改系统在通过双击执行批处理文件时使用的cmd.exe会话的属性…@Aacini-有一个选项可以自动保存双击快捷方式的设置。我不记得我做了什么,但很简单。但是,在重新阅读OP的问题时,我发现缓冲区已经用完了,因此我的建议在这种情况下没有帮助。stderr需要重定向到stdout 2>&1,OP可能更喜欢简单地键入结果日志。好的观点是Dave。我已经添加了STDERR重定向。OP提到CMD缓冲区对于他的脚本输出来说不够大,因此键入该文件将导致相同的问题:他们也可以用更多的字体来代替字体。
C:\Documents and Settings\Antonio\My Documents\test
>if defined Restart goto Begin

C:\Documents and Settings\Antonio\My Documents\test
>rem TeeMyself.bat: Send output of this Batch file into screenOut.txt file

C:\Documents and Settings\Antonio\My Documents\test
>rem Antonio Perez Ayala

C:\Documents and Settings\Antonio\My Documents\test
>set Restart=True

C:\Documents and Settings\Antonio\My Documents\test
>call "C:\Documents and Settings\Antonio\My Documents\test\TeeMyself.bat"   2>&1  | Cscript //nologo //E:JScript "C:\Documents and Settings\Antonio\My Documents
\test\TeeMyself.bat"  2>screenOut.txt

===================================================
 The same contents of screenOut.txt file goes here
===================================================

C:\Documents and Settings\Antonio\My Documents\test
>set Restart=

C:\Documents and Settings\Antonio\My Documents\test
>goto :EOF