0

I want to show a busy spinner until an exe is not shown in tasklist. I'm checking a system for a specific version of JRE and installing it if it isn't there. It takes about 45 seconds to install in silent mode and I'm also installing 2 other programs after JRE is installed that are .jar files and are dependent on JRE being installed. Here is my code so far to just install JRE:

@echo off
setlocal

call :FindJRE jre
exit /b

If not %jre% EQU dec (
    echo Setting Java home path.
    Call :SetJavaHome
    if exist "C:\Program Files\Java\jre7\bin\javaw.exe" echo Java installed
)   ELSE ( Endlocal&echo You must install Java Runtime Environment for Hermes to work.&exit /b 1)   
exit /b 

:FindJRE
:: Locate existing Java Runtime Environment 
:: or install if not found.
@echo on
setlocal enabledelayedexpansion
set "aux1= "
set "KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
FOR /F "usebackq skip=4 tokens=3" %%A IN (
    `REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>nul`
    ) DO ( set "Val=%%~A" )
        if defined Val (
                echo.the current Java runtime is %Val%
        ) else ( 
                echo.Java Runtime Environment not found.
                        for /f %%a in ('dir /b jre-7u51*') do (
                                Echo.Attempting to install Java Runtime Environment. 
                                set "loc=%%a"
                                        If defined loc ( 
                                            echo.Flipping the junkware bit
                                            REG add HKLM\SOFTWARE\JavaSoft /v Sponsors /t REG_SZ /d Disable /f>nul
                                            !loc! /s WEB_JAVA_SECURITY_LEVEL=VH SPONSORS=0
                                            Set Key2="HKLM\SOFTWARE\JavaSoft"
                                                FOR /F "usebackq skip=4 tokens=3" %%G IN (
                                                    `REG QUERY !Key2! /v InstallStatus 2^>nul`) do (
                                                        If "%%G" EQU "decline" (
                                                            Echo JRE install cancelled
                                                            Endlocal&Set %~1=dec&exit /b 1
                                                        )
                                                )

                                        ) ELSE (
                                                Echo.Java installer not found. Please download the latest Java Runtime Environment from the Oracle Website.
                                                exit /b 1
                                        )       
                        )
        )
FOR /F "usebackq skip=4 tokens=3*" %%A IN (
    `REG QUERY "%KEY_NAME%\%Val%" /v JavaHome 2^>nul`
    ) DO ( 
    set "aux1=%%~A %%~B"
)
endlocal&set %~1="%aux1%"&exit /b 0 

:SetJavaHome
@echo on
setlocal enabledelayedexpansion
set "aux1= "
set "KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
FOR /F "usebackq skip=4 tokens=3" %%A IN (
    `REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>nul`
    ) DO ( set "Val=%%~A" )
        if defined Val (
                echo.the current Java runtime is %Val%
        )
FOR /F "usebackq skip=4 tokens=3*" %%C IN (
    `REG QUERY "%KEY_NAME%\%Val%" /v JavaHome 2^>nul`
) DO ( 
    set aux1="%%~C %%~D"
)   
set key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Echo Adding Java_Home environment variable
reg add %key% /v JAVA_HOME /t REG_SZ /d %aux1% /f >nul
For /f "skip=4 tokens=3*" %%a in ('reg query %key% /v JAVA_HOME') do (
    Echo Java home path set to: %%a %%b
)
exit /b

Simple Working Example

@Echo off
SetLocal EnableExtensions EnableDelayedExpansion

For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set /A n=0

::BUSY SPINNER
For /L %%i in (0,1,20) Do (
 Set /A "n=%%i%%4"
 For /L %%n in (!n! 1 !n!) Do Set /P "=Calculating !busy:~%%n,1! !CR!"<NUL:
 PING -n 1 127.0.0.1 >NUL:
)

Latest Code

This is sort of working now but there is an annoying screen flash. Still working on it.

@Echo off
SetLocal EnableExtensions EnableDelayedExpansion

For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set /A n=0
set "exe=notepad.exe"
start "" %exe%
::BUSY SPINNER
:loop
for /f %%a in ('tasklist /V /FI "Imagename eq %exe%" /FO CSV /NH^|find /i "%exe%"') do (
    if not errorlevel 1 (
        set /a count+=1
        if !count!==4 set /a count=0
        Set /A "n=!count!%%4"
        For /L %%n in (!n! 1 !n!) Do Set /P "=Calculating !busy:~%%n,1! !CR!"<NUL:
        PING -n 1 127.0.0.1 >NUL:
        goto :Loop
    ) ELSE ( echo JRE installed. )
)
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
  • possible duplicate of [How to code a spinner for waiting processes in a Batch file?](http://stackoverflow.com/questions/368041/how-to-code-a-spinner-for-waiting-processes-in-a-batch-file) – aphoria Mar 13 '15 at 18:18
  • @aphoria I saw both of those but they're more code than necessary now. It's quite simple to do it using a for /L but I can't figure out how to adapt it for checking if a process is running via taskmanager or wmic. – Matt Williamson Mar 13 '15 at 18:23
  • @aphoria That question and those answers are 7 years old. We've evolved a bit since then. ;) – Matt Williamson Mar 13 '15 at 18:44
  • Yes, it's an old question and answer, but the batch language hasn't changed since then either. :) You could use `TASKLIST` to check for a running process instead of using a flag file. But, what will happen when your `For /L %%i in (0,1,20) Do (` loop ends but your task is still running? – aphoria Mar 13 '15 at 18:55
  • @aphoria I've already tried replacing that for /L loop with one that checks tasklist instead and uses a counter variable inside but it isn't doing what I expect it should. I've updated the post to show where I am so far. – Matt Williamson Mar 13 '15 at 19:04

1 Answers1

1

Try this:

@Echo off
setLocal EnableDelayedExpansion

For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set n=0
set "exe=notepad.exe"
start "" %exe%

::BUSY SPINNER
:loop
tasklist /FI "Imagename eq %exe%" /FO CSV /NH 2>NUL | find /i "%exe%" > NUL
if errorlevel 1 goto endProg
   set /A "n=(n+1)%%4"
   set /P "=Calculating !busy:~%n%,1!!CR!" < NUL
   PING -n 1 127.0.0.1 > NUL
goto :Loop

:endProg
echo JRE installed.
Aacini
  • 65,180
  • 12
  • 72
  • 108