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. )
)