windows 批处理脚本的日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24521687/
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 11:38:29  来源:igfitidea点击:

Log file for batch script

windowsbatch-filewindows-server-2003-r2

提问by Mike G

I'm a beginner in scripting, I just made a batch script to create some quotas on a server Win2003. I would like to have a log file as an output , but the only command I know is >>logfile.txt which will make a log file for each command.

我是脚本的初学者,我刚刚制作了一个批处理脚本来在服务器 Win2003 上创建一些配额。我想要一个日志文件作为输出,但我知道的唯一命令是 >>logfile.txt,它将为每个命令创建一个日志文件。

I would like to have ONE logfile.txt that will log if all commands were successfully applied or not...

我想要一个 logfile.txt 来记录所有命令是否成功应用...

Can someone help please with the coding please?

有人可以帮忙编码吗?

@echo off
echo.
Dirquota quota add "J:\P1\BD OG" /Limit:60GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "J:\P1\BD Chair" /Limit:60GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "M:\P2\BD Arena" /Limit:50GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "K:\P3\BD Home" /Limit:30GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
echo.
pause

回答by Magoo

@ECHO OFF
SETLOCAL
(
 FOR /f "tokens=1-4" %%a IN (q24521687.txt) DO (
  ECHO(Dirquota quota add "%%a:\%%b\BD %%c" /Limit:%%dGB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
  IF ERRORLEVEL 1 (ECHO failed) ELSE (ECHO succeeded)
 )
)>newfile.txt

GOTO :EOF

This batch routine uses a file named q24521687.txtcontaining this data (derived from yours)

此批处理例程使用名为q24521687.txt包含此数据的文件(源自您的)

J P1 OG 60
J P1 Chair 60
M P2 Arena 50
K P3 Home 30

Which may be easier to maintain.

这可能更容易维护。

Produces newfile.txt - this could be any filename you choose. Use >>in place of >to append to an existing file (or create it if it doesn't exist) >will replace any existing file.

生成 newfile.txt - 这可以是您选择的任何文件名。使用>>代替>附加到现有文件(如果不存在则创建它)>将替换任何现有文件。

Note that this will simply ECHOthe dirquotacommand-line for checking (to the newfile.txtfile). Delete the echo(to invoke the executable.

请注意,这将只是用于检查(到文件)ECHOdirquota命令行newfile.txt。删除echo(以调用可执行文件。

I've assumed that dirquotauses the standard errorlevelsetting - 0 for success, non-zero otherwise. Without your specifying what the termination condition for dirquotais, I would be guessing.

我假设dirquota使用标准errorlevel设置 - 0 表示成功,否则为非零。如果没有您指定终止条件dirquota是什么,我会猜测。



Response to comments:

对评论的回应:

There's no indication about BD- is it part of everyname to be constructed in that position, or only some?

没有任何迹象表明BD- 它是在该位置构建的每个名称的一部分,还是只有一些

The for /fparses the lines of the input file and assigns each token on the line to %%a..%%d(%%ais nominated, tokens 1-4 are selected, so each token on the input line is assigned to the next-alphabetical metavariable).

for /f解析输入文件的行和受让人每个令牌上线%%a..%%d%%a提名,令牌1-4被选择,所以每个标记在输入线被分配给下一个字母metavariable)。

The default separators are Spaceand comma, amongst others. Consequently, J P1 OG 60for example would assign Jto %%a, P1to %%b, OGto %%cand 60to %%d. Hence, the dirquotaline would be constructed as required, assuming(for lack of data otherwise) that all directorynames are of the form BD something.

默认分隔符是Space和逗号等。因此,J P1 OG 60例如将分配J%%aP1%%bOG%%c60%%d。因此,该dirquota行将根据需要构造,假设(否则缺少数据)所有目录名称的形式都是BD something.

With some small adjustments, this can be extended.

通过一些小的调整,这可以扩展。

Suppose you wanted directorynames BD OG, sausagesand XY Table.

假设您想要目录名BD OG,sausagesXY Table.

Since the directory name may or may not contain a space, we could fix the routine by changing it a little:

由于目录名称可能包含也可能不包含空格,我们可以通过稍微更改它来修复例程:

@ECHO OFF
SETLOCAL
(
 FOR /f "tokens=1-4DELIMS=," %%a IN (q24521687.txt) DO (
  ECHO(Dirquota quota add "%%a:\%%b\%%c" /Limit:%%dGB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
  IF ERRORLEVEL 1 (ECHO %%c failed) ELSE (ECHO %%c succeeded)
 )
)>newfile.txt

GOTO :EOF

(note the changes : remove the BDSpacefrom the directory in the dirquotaline and add the delims=,phrase into the for /f)

(注意更改:从行中的目录中删除BDSpacedirquota并将delims=,短语添加到for /f

And our data in the text file becomes

而我们在文本文件中的数据就变成了

J,P1,BD OG,60
J,P1,sausages,60
M,P2,XY Table,50
K,P3,BD Home,30

Simply each variable part separated by commas (the delims=character selected).

只需用逗号分隔每个变量部分(delims=所选字符)。

Note the positioning of the quotes. These are designed to ensure that separators are correctly processed.

注意引号的位置。这些旨在确保正确处理分隔符。

As presented, since the dirquotacommand is simply echoed, not executed, errorlevelwill be 0so each line will be reported as having succeeded (I've added %%cto the succeeded/failed report). Once you've checked that the dirquotacommand-lines being reported are correct, simply change ECHO(Dirquotato Dirquotaand the dirquotalines will be executed; Assuming dirquotais an executable and it sets errorlevelto the standard 0=success;otherwise failurethen the if errorlevelline directly following the dirquotaline should correctly display the result.

作为呈现,因为dirquota命令被简单地echo编,不执行,errorlevel就会0使每一行会被报告为成功(我已经添加%%c到成功/失败的报告)。一旦您检查了dirquota报告的命令行是否正确,只需更改ECHO(DirquotaDirquota,这些dirquota行就会被执行;假设dirquota是一个可执行文件并且它设置errorlevel为标准,0=success;otherwise failure那么if errorlevel紧跟在该行后面的dirquota行应该正确显示结果。

回答by dbenham

As Aacini says in his comment, you can simply redirect output when you invoke your batch file. If your batch script is called "myScript.bat", then you can use myScript >myLog.txt 2>&1. If you want to append to an existing log file, then use myScript >>myLog.txt 2>&1.

正如 Aacini 在他的评论中所说,您可以在调用批处理文件时简单地重定向输出。如果您的批处理脚本名为“myScript.bat”,那么您可以使用myScript >myLog.txt 2>&1. 如果要附加到现有日志文件,请使用myScript >>myLog.txt 2>&1.

The funny looking redirection at the end causes error messages on stderr to be redirected to the same location as stdin, which is the log file in this case. This is needed so that you can capture any error messages that DIRQUOTA may produce.

最后有趣的重定向导致 stderr 上的错误消息被重定向到与 stdin 相同的位置,在这种情况下是日志文件。这是必需的,以便您可以捕获 DIRQUOTA 可能产生的任何错误消息。

There is one problem with this approach - Your PAUSE output will also appear in the log file, and you will not see the prompt to press a key on the screen. You could solve this by obtaining a Windows port of the TEE utility that sends output to both the screen and a file.

这种方法有一个问题 - 您的 PAUSE 输出也会出现在日志文件中,并且您不会在屏幕上看到按下按键的提示。您可以通过获取将输出发送到屏幕和文件的 TEE 实用程序的 Windows 端口来解决此问题。

Alternatively, you could modify your script as follows, and call it normally:

或者,您可以按如下方式修改您的脚本,并正常调用它:

@echo off
call :main >myLog.txt 2>&1
pause
exit /b

:main
echo.
Dirquota quota add "J:\P1\BD OG" /Limit:60GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "J:\P1\BD Chair" /Limit:60GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "M:\P2\BD Arena" /Limit:50GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "K:\P3\BD Home" /Limit:30GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
echo.
exit /b