Bat文件在echo处停止执行

时间:2015-09-11 16:20:55

标签: batch-file

Windows 7上的我的bat文件在echo行停止执行。我必须按ENTER才能继续。例如,它将暂停在行:

echo Stopping service.

几乎相同的脚本文件可以根据需要从头到尾运行良好。

编码是ANSI。在Windows 7 64位上运行。

我错过了什么?

完整示例:

@echo off

set _JAVA_OPTIONS="-Djava.net.preferIPv4Stack=true"

rem Make sure user is running this as an admin
rem ref http://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
net session >nul 2>&1
if %errorLevel% == 0 (
rem echo Administrative permissions confirmed.

rem %~dp0 will give you the full path to the batch file's directory (fixed

    IF EXIST "%~dp0"\Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\stopService.bat (
rem echo Found required file %~dp0Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\stopService.bat
    ) ELSE (
    echo Required file %~dp0Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\stopService.bat was not found!
    GOTO:EOF
    )

    IF EXIST "%~dp0"\Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\uninstallService.bat (
rem echo Found required file %~dp0Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\uninstallService.bat
    ) ELSE (
    echo Required file %~dp0Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\uninstallService.bat was not found!
    GOTO:EOF
    )

    IF EXIST "%~dp0"\Service\yajsw-beta-12.01\yajsw-beta-12.01\conf\wrapper.conf (
rem echo Found required file %~dp0Service\yajsw-beta-12.01\yajsw-beta-12.01\conf\wrapper.conf
    ) ELSE (
    echo Required file %~dp0Service\yajsw-beta-12.01\yajsw-beta-12.01\conf\wrapper.conf was not found!
    GOTO:EOF
    )

    echo Stopping service. 

"%~dp0"\Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\stopService.bat >nul 2>&1
    @echo Uninstalling service. One moment...
rem  Sleep   ref http://stackoverflow.com/questions/4527877/batch-script-read-line-by-line

    ping -n 4 127.0.0.1 >nul

    "%~dp0"\Service\yajsw-beta-12.01\yajsw-beta-12.01\bat\uninstallService.bat >nul 2>&1

    cd %~dp0

    echo Completed. 
    echo The LCSC service has been removed from 
    echo Control Panel ^> Administrative Tools ^> Windows Services 
) else (
    echo No can do!
    echo Please execute this file as a Windows administrator 
    echo by clicking
    echo Start button ^> All Programs ^> Accessories ^> 
    echo then right click on the command prompt icon and select
    echo "Run as an Administrator"
    pause
    GOTO:EOF
)

TIA, 贝特朗

1 个答案:

答案 0 :(得分:0)

这是您的批处理文件,但有一些改进:

@echo off
setlocal
set "BatchFolder=%~dp0"
set "ServiceFolder=%BatchFolder%Service\yajsw-beta-12.01\yajsw-beta-12.01"
set "_JAVA_OPTIONS=-Djava.net.preferIPv4Stack=true"

rem Make sure user is running this as an admin
rem ref http://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
%SystemRoot%\System32\net.exe session >nul 2>&1

if %errorLevel% == 0 (

    rem echo Administrative permissions confirmed.

    rem %~dp0 will give you the full path to the batch file's directory (fixed

    if exist "%ServiceFolder%\bat\stopService.bat" (
        rem echo Found required file %ServiceFolder%\bat\stopService.bat
    ) else (
        echo Required file %ServiceFolder%\bat\stopService.bat was not found!
        goto EndBatch
    )

    if exist "%ServiceFolder%\bat\uninstallService.bat" (
        rem echo Found required file %ServiceFolder%\bat\uninstallService.bat
    ) else (
        echo Required file %ServiceFolder%\bat\uninstallService.bat was not found!
        goto EndBatch
    )

    if exist "%ServiceFolder%\conf\wrapper.conf" (
        rem echo Found required file %ServiceFolder%\conf\wrapper.conf
    ) else (
        echo Required file %ServiceFolder%\conf\wrapper.conf was not found!
        goto EndBatch
    )

    echo Stopping service.
    call "%ServiceFolder%\bat\stopService.bat" >nul 2>&1

    echo Uninstalling service. One moment...
    rem  Sleep ref http://stackoverflow.com/questions/4527877/batch-script-read-line-by-line

    %SystemRoot%\System32\ping.exe -n 4 127.0.0.1 >nul

    call "%ServiceFolder%\bat\uninstallService.bat" >nul 2>&1

    cd /D "%BatchFolder%"

    echo Completed.
    echo The LCSC service has been removed from
    echo Control Panel ^> Administrative Tools ^> Windows Services

) else (
    echo No can do!
    echo Please execute this file as a Windows administrator
    echo by clicking
    echo Start button ^> All Programs ^> Accessories ^>
    echo then right click on the command prompt icon and select
    echo "Run as an Administrator"
    endlocal
    pause
    goto :EOF
)

:EndBatch
endlocal

Stopping service.打印到屏幕后需要按键的原因可能是命令pause或任何其他命令请求用户输入或用户通过批处理文件中的键确认{{ 1}}在行stopService.bat

之后调用

由于echo Stopping service.调用批处理文件,因此在控制台窗口中不会显示命中键的请求。

因此,请从

行中删除>nul 2>&1
>nul 2>&1

并运行批处理文件以查看是否输出了请求按键的用户提示。