首页 > 解决方案 > 如何在命令提示符下将整个输出发送到日志文件以显示时间更改和错误

问题描述

以下代码通过处理每个命令并等待来完成我需要做的事情。但是,我似乎无法弄清楚如何将所有输出放入日志文件。现在它只记录日期和时间。我需要将 cmd 中的所有输出(包括任何错误)记录到日志文件中。我将不胜感激这方面的所有帮助。

echo Logged time = %time% %date%>> TaskSetBatchScripts.log

@echo off
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitor
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitorToastTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyRefreshTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "\Microsoft\Windows\Power Efficiency Diagnostics\AnalyzeSystem"
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsToastTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsUpdateTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "\Microsoft\Windows\Mobile Broadband Accounts\MNO Metadata Parser"
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "\Microsoft\Windows\Windows Error Reporting\QueueReporting"
timeout 5 > NUL
echo %time%

schtasks /Change /disable/TN \Microsoft\Windows\Defrag\ScheduledDefrag
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "Microsoft\Windows\Windows Media Sharing\UpdateLibrary"
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN Microsoft\Windows\Maintenance\WinSAT 
timeout 5 > NUL
echo %time%

标签: windowsbatch-filecmdio-redirection

解决方案


实现这一点,非常容易。有两种方法:在批处理文件中进行:

setlocal EnableDelayedExpansion
(
echo Logged time = !time! !date!

@echo off
echo !time

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitor
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitorToastTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyRefreshTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Power Efficiency Diagnostics\AnalyzeSystem"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsToastTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsUpdateTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Mobile Broadband Accounts\MNO Metadata Parser"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Windows Error
Reporting\QueueReporting"
timeout 5 > NUL
echo !time!

schtasks /Change /disable/TN \Microsoft\Windows\Defrag\ScheduledDefrag
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Windows MediaSharing\UpdateLibrary"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maintenance\WinSAT 
timeout 5 > NUL
echo !time!
)>>TaskSetBatchScripts.log 2>&1

不确定您需要什么:追加(>>)或重定向(>)[删除以前的内容]。

第二种方法是从 cmd 运行文件,如下所示:

(filename.bat)>>TaskSetBatchScripts.log 2>&1

再次不确定您需要什么。

(第三种方法是将每个命令的输出附加到文件添加2>&1)。


推荐阅读