【问题标题】:Bat file stops executing at echoBat 文件在 echo 处停止执行
【发布时间】:2015-09-12 04:33:53
【问题描述】:

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

echo Stopping service.

一个几乎相同的脚本文件从头到尾都可以正常运行。

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

我错过了什么?

完整示例:

@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, 伯特兰

【问题讨论】:

  • 我可能有点脱离 .bat 文件的循环,但是在您访问另一个 .bat 文件的行中,您确定另一个返回吗? ISTR 您需要使用 call 或其他东西来调用另一个 .bat 文件作为子进程。
  • 好像没什么区别?
  • @MrLister 是 100% 正确的,请确保对您的 bat 文件的所有调用都以 call 开头。为什么当你点击 ENTER 时它会继续是一件奇怪的事情,也许是你正在调用的程序中的一些东西。如果是这样,您可以将包含单个 CRLF 的文件重定向到它们。
  • 您可能在文件夹或影响它的路径中有一个 echo.bat/cmd/com/exe。

标签: batch-file


【解决方案1】:

这是您的批处理文件,经过一些改进:

@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 或在批处理文件stopService.bat 之后调用的请求用户输入或用户确认的任何其他命令线echo Stopping service.

由于>nul 2>&1在调用批处理文件时,控制台窗口中不显示按键请求。

因此从行中删除>nul 2>&1

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2016-10-06
    • 2018-06-09
    • 2012-05-17
    相关资源
    最近更新 更多