Errorlevel变量在错误后没有改变..?

时间:2017-07-03 20:00:13

标签: batch-file errorlevel

我正在尝试制作一个在后台不断运行的脚本。其目标是检测USB闪存驱动器,外部硬盘驱动器或您连接到计算机的任何可写设备。一旦检测到一个,它就会复制该设备根目录下的文件。这是我的代码(回声线用于更清晰的调试):

@echo off
:loop
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
    for %%c in (%%b) do (
        for /F "tokens=4" %%d in ('fsutil fsinfo drivetype %%c') do (
            if %%d equ amovible (
                echo Found out that %%c is removable. Trying to echo the content...
                dir %%c >NUL
                echo Error: %errorlevel%
                if %errorlevel%==0 (
                    echo Errorlevel is 0! Copying...
                    if not exist %%c\test.txt (
                        copy .\test.txt %%c\test.txt
                        echo.
                    )
                ) else (
                    echo Errorlevel isn't 0. Looks like the drive isn't here, or is unavailable. Aborting copying...
                  echo.
                )
            )
        )
    )
)
TIMEOUT 2
goto :loop

这就是问题所在。条件"如果%errorlevel%== 0"应该检查" dir %% c> NUL"命令成功,所以如果errorlevel为0,则表示设备上有文件系统,并且它不像空读卡器或空软盘驱动器。问题是当我执行我的代码时,errorlevel总是0,我无法弄清楚原因(" echo错误:%errorlevel%"告诉它)。因此文件test.txt会复制到我的USB闪存驱动器上,所以这基本上工作,就是将文件复制到我的软盘驱动器时出现错误框,说我应该在阅读器中插入一个设备那就是我想要做错误检查的原因。

所以,如果有人有解决方案,请帮忙,我真的卡住了。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您需要使用delayed expansion!ErrorLevel!),否则,您将获得整个带括号的块(即嵌套ErrorLevel时出现的for值这里的循环结构)被解析。由于延迟扩展功能会消耗感叹号,因此我只会在实际需要时启用它,如下所示:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
:loop
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
    for %%c in (%%b) do (
        for /F "tokens=4" %%d in ('fsutil fsinfo drivetype %%c') do (
            if "%%d"=="amovible" (
                echo Found out that %%c is removable. Trying to echo the content...
                dir "%%~c" > nul
                setlocal EnableDelayedExpansion
                echo Error: !ErrorLevel!
                if !ErrorLevel! equ 0 (
                    endlocal
                    echo ErrorLevel is 0! Copying...
                    if not exist "%%~c\test.txt" (
                        copy ".\test.txt" "%%~c\test.txt"
                        echo/
                    )
                ) else (
                    endlocal
                    echo ErrorLevel isn't 0. Looks like the drive isn't here, or is unavailable. Aborting copying...
                    echo/
                )
            )
        )
    )
)
timeout 2
goto :loop
endlocal

或者,由于dir从未将ErrorLevel设置为负值,因此您也可以使用if not errorlevel 1 syntax,意思是"如果不是ErrorLevel则大于或等于1",因此"如果ErrorLevel小于1" (显然你不能回应实际的ErrorLevel值):

                ...
                dir "%%~c" > nul
                if not ErrorLevel 1 (
                    echo ErrorLevel is 0! Copying...
                    if not exist "%%~c\test.txt" (
                        copy ".\test.txt" "%%~c\test.txt"
                        echo/
                    )
                ) else (
                    echo ErrorLevel isn't 0. Looks like the drive isn't here, or is unavailable. Aborting copying...
                    echo/
                )
                ...

另一种方法是使用conditional execution operators && and/or ||,它允许以下命令或块仅在前面的命令返回零退出代码的情况下执行(大部分时间,以及dir在这里命令,退出代码与ErrorLevel)的值相同:

                ...
                dir "%%~c" > nul && (
                    echo ErrorLevel is 0! Copying...
                    if not exist "%%~c\test.txt" (
                        copy ".\test.txt" "%%~c\test.txt"
                        echo/
                    )
                ) || (
                    echo ErrorLevel isn't 0. Looks like the drive isn't here, or is unavailable. Aborting copying...
                    echo/
                )
                ...