发生下载错误时如何强制退出命令行bat文件

时间:2017-03-28 03:03:13

标签: batch-file

逗人 这是我的.bat命令行 我从url使用bitsadmin / transfer命令下载。 但是得到了错误(比如断开网络...等)我需要继续执行保持命令。

但现在我无法获得成就......我该怎么做

@echo off
:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates are the exact same file, downloaded from the same location!
SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET "destination=C:\FilterUpdates.cab"

if not exist "%DTMBIN%\" (
    echo ERROR: folder "%DTMBIN%"
    echo does not exist! Please verify that you have the controller installed.
    pause
    exit /B 1
)

echo Please make sure that all instances of the Studio are turned OFF!


echo Downloading Filters...
bitsadmin /transfer "Downloading Filters" "%source%" "%destination%"
if errorlevel 1 goto end
echo Extracting...
expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
echo Installing...
pushd "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
"%DTMBIN%\updatefilters.exe " /s
if not errorlevel 0 echo ERROR & exit /B 1
popd

:end
exit

2 个答案:

答案 0 :(得分:1)

您的问题可能是由bitsadmin生成的错误级别引起的:其中一些是负值,而测试if errorlevel 1将被评估为false(if errorlevel n对于大于或等于n

您需要阅读并测试errorlevel变量的值

if not %errorlevel%==0 exit /b 1

但有时,bitsadmin会有错误并会生成errorlevel 0,因此,您需要手动检查状态

@echo off
    setlocal enableextensions disabledelayedexpansion

:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates 
:: are the exact same file, downloaded from the same location!

    SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    if not exist "%DTMBIN%\" (
        echo ERROR: folder "%DTMBIN%"
        echo does not exist! Please verify that you have the controller installed.
        pause
        exit /B 1
    )

    SET "destination=C:\FilterUpdates.cab"
    if exist "%destination%" del /q "%destination%"

    echo Please make sure that all instances of the Studio are turned OFF!

    echo Creating download task...
    set "taskName=[HCK_FilterUpdater]"

    >nul (
        rem remove task if already present
        bitsadmin /list | find "%taskName%" && bitsadmin /cancel "%taskName%"
        rem create the task
        bitsadmin /create "%taskName%" 
        rem include our file in the task
        bitsadmin /addfile "%taskName%" "%source%" "%destination%"
        rem start the download
        bitsadmin /resume "%taskName%"
    )

    echo Downloading...
    set "exitCode="
    for /l %%a in (1 1 500) do if not defined exitCode for /f "delims=" %%a in ('
        bitsadmin /info "%taskName%" 
        ^| findstr /b /l /c:"{"
    ') do for /f "tokens=3,*" %%b in ("%%a") do (
        if "%%~b"=="TRANSFERRED" ( 
            set "exitCode=0"
            >nul bitsadmin /complete "%taskName%"
            echo ... done
        )
        if "%%~b"=="ERROR" ( 
            set "exitCode=1"
            bitsadmin /geterror "%taskName%" | findstr /b /c:"ERROR"
            >nul bitsadmin /cancel "%taskName%"
        )
        if not defined exitCode (
            echo(%%b %%c
            timeout /t 2 >nul 
        )
    )
    if not defined exitCode ( echo TIMEOUT & exit /b 1 )
    if not exist "%destination%" ( echo ERROR & exit /b 1 )

    echo Expanding...
    >nul expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%"
    if errorlevel 1 ( echo ERROR & exit /b 1 )

    echo Installing...
    pushd "%DTMBIN%"          || ( echo ERROR & exit /b 1 )
    ".\updatefilters.exe " /s || ( echo ERROR & exit /b 1 )
    popd

答案 1 :(得分:-1)

似乎一旦命令 shell 处于 bitsadmin 模式,您就无法将其取出。不幸的是,我的解决方案是两个单独的 .bat。