1

My bat file on Windows 7 stops executing at echo lines. I have to hit ENTER to continue. For example it will pause at line:

echo Stopping service.

An almost identical script file runs fine from beginning to end as desired.

Encoding is ANSI. Running on Windows 7 64-bits.

What am I missing?

Complete example:

@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, Bertrand

Quadmore
  • 35
  • 6
  • 2
    I may be a bit out of the loop with .bat files, but in the line where you access the other .bat file, are you sure the other one returns? ISTR you need to use `call` or something to call another .bat file as a subprocess. – Mr Lister Sep 11 '15 at 16:27
  • Does not seem to make a difference? – Quadmore Sep 11 '15 at 16:44
  • @MrLister is 100% correct, make sure all of the calls to your bat files are preceded by call. Why it would continue when you hit ENTER is an odd thing, maybe something in the programs you are calling. If so, you can redirect a file with a single CRLF in it to them. – John Kuhns Sep 11 '15 at 16:52
  • You probably have an echo.bat/cmd/com/exe in the folder or on the path that is affecting it. – foxidrive Sep 11 '15 at 19:09

1 Answers1

0

Here is your batch file with some improvements:

@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

The reason for the need of hitting a key after Stopping service. is printed to screen could be a command pause or any other command requesting a user input or user confirmation by key in the batch file stopService.bat called after the line echo Stopping service.

The request to hit a key is not displayed in console window because of >nul 2>&1 on calling the batch file.

Therefore remove >nul 2>&1 from line

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

and run the batch file to see if a user prompt is output requesting pressing a key.

Mofi
  • 46,139
  • 17
  • 80
  • 143