简体   繁体   English

由批处理循环执行的命令的错误级别

[英]Errorlevel of command executed by batch for loop

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作为errorlevel,但是当复制命令在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循环执行的复制命令的错误级别?

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 \\ _27691_Scan.pdf) ,我可以运行以下批处理文件

@echo off

setlocal EnableDelayedExpansion

for /F "delims=" %%I in (CopyCurrentPDFs.txt) do (
%%I
if !errorlevel! NEQ 0 echo %%I>>errorcopy.txt
)

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. 如果那不是您想要做的,请澄清您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM