windows 批处理 for 循环执行的命令的错误级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3088712/
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
Errorlevel of command executed by batch for loop
提问by Josh
The following code always displays 0 as the errorlevel, but when the copy command is done outside of the for loop command it returns a non zero errorlevel.
下面的代码总是显示 0 作为错误级别,但是当复制命令在 for 循环命令之外完成时,它返回一个非零错误级别。
for /f "usebackq delims=" %%x in (`copy x y`) do (
set VAR=%%x
)
ECHO Errorlevel = %ERRORLEVEL%
ECHO VAR = %VAR%
Is is possible to get the errorlevel of the copy command executed by the for loop?
是否可以获得for循环执行的复制命令的错误级别?
回答by Gepi
it works for me ! You only need to put the error checking within the DO parentheses with a text file containing the copy commands (7200 lines; for example: copy 2_97691_Scan.pdf O:\Data\Dev\Mins\PDFScan2\2011\4\2_97691_Scan.pdf), I can run the following batch file
这个对我有用 !您只需要将错误检查放在包含复制命令的文本文件的 DO 括号内(7200 行;例如:copy 2_97691_Scan.pdf O:\Data\Dev\Mins\PDFScan2\2011\4\2_97691_Scan.pdf) ,我可以运行以下批处理文件
@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%I in (CopyCurrentPDFs.txt) do (
%%I
if !errorlevel! NEQ 0 echo %%I>>errorcopy.txt
)
回答by WildCrustacean
I am assuming that you are copying files from one directory to another? If so, you could do something like this instead:
我假设您正在将文件从一个目录复制到另一个目录?如果是这样,你可以这样做:
@echo off
setlocal EnableDelayedExpansion
set ERR=0
for %%x in (x) do (
copy %%x y
set ERR=!errorlevel!
set VAR=%%x
)
ECHO Errorlevel = %ERR%
ECHO VAR = %VAR%
The delayed expansion is required to get the actual value of errorlevel inside the loop instead of the value before the loop is entered.
需要延迟扩展才能得到循环内部errorlevel的实际值,而不是进入循环前的值。
If that isn't what you are trying to do, please clarify your objective.
如果这不是您想要做的,请阐明您的目标。