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
Log file for batch script
提问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.txt
containing 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 ECHO
the dirquota
command-line for checking (to the newfile.txt
file). Delete the echo(
to invoke the executable.
请注意,这将只是用于检查(到文件)ECHO
的dirquota
命令行newfile.txt
。删除echo(
以调用可执行文件。
I've assumed that dirquota
uses the standard errorlevel
setting - 0 for success, non-zero otherwise. Without your specifying what the termination condition for dirquota
is, 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 /f
parses the lines of the input file and assigns each token on the line to %%a..%%d
(%%a
is 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 60
for example would assign J
to %%a
, P1
to %%b
, OG
to %%c
and 60
to %%d
. Hence, the dirquota
line 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
给%%a
、P1
给%%b
、OG
给%%c
和60
给%%d
。因此,该dirquota
行将根据需要构造,假设(否则缺少数据)所有目录名称的形式都是BD something
.
With some small adjustments, this can be extended.
通过一些小的调整,这可以扩展。
Suppose you wanted directorynames BD OG
, sausages
and XY Table
.
假设您想要目录名BD OG
,sausages
和XY 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 dirquota
line 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 dirquota
command is simply echo
ed, not executed, errorlevel
will be 0
so each line will be reported as having succeeded (I've added %%c
to the succeeded/failed report). Once you've checked that the dirquota
command-lines being reported are correct, simply change ECHO(Dirquota
to Dirquota
and the dirquota
lines will be executed; Assuming dirquota
is an executable and it sets errorlevel
to the standard 0=success;otherwise failure
then the if errorlevel
line directly following the dirquota
line should correctly display the result.
作为呈现,因为dirquota
命令被简单地echo
编,不执行,errorlevel
就会0
使每一行会被报告为成功(我已经添加%%c
到成功/失败的报告)。一旦您检查了dirquota
报告的命令行是否正确,只需更改ECHO(Dirquota
为Dirquota
,这些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