繁忙的微调器,用于程序安装的持续时间

时间:2015-03-13 17:49:32

标签: batch-file

我希望显示一个忙碌的微调器,直到任务列表中没有显示exe。我正在检查系统是否有特定版本的JRE,如果不在那里就安装它。在静默模式下安装大约需要45秒,并且在安装JRE之后我还安装了2个其他程序,这些程序是.jar文件,并且依赖于正在安装的JRE。到目前为止,我的代码只是安装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

简单工作示例

@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:
)

最新代码

现在有点工作,但有一个恼人的屏幕闪光灯。仍在努力。

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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