2015-11-25 41 views
1

I'have用DOS批處理,我要用來安裝java一個問題:DOS子和變量

REM SCRIPT FOR UPDATING JAVA... THERE'S NOTHING TO EDIT, ONLY COPY THE JRE EXE INSTALLATION FILES INTO THE SAME DIR OF THE SCRIPT 

@echo off 

set ProductName=jre 
set OS=windows 
set FileType=exe 
set LogLocation=.\log 
set Parameters=/s INSTALL_SILENT=1 WEB_JAVA_SECURITY_LEVEL=H AUTO_UPDATE=0  WEB_ANALYTICS=0 REBOOT=0 /L %LogLocation%\%COMPUTERNAME%_Install.log 
set Uninstall=%SystemRoot%\system32\msiexec.exe /qn /norestart /l* %LogLocation%\%COMPUTERNAME%_Uninstall.log /x 
set Version= 
set Maj= 
set Min= 

Pushd "%~dp0" 

mkdir %LogLocation% 


echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 

IF NOT "%ProgramFiles(x86)%"=="" (
    echo This is a 64bit system. Installing both 32bit and 64bit version.... 
    call :SETVAR64 
    call :INSTALL 
    call :SETVAR32 
    set Wow=WOW6432NODE\ 
    call :INSTALL 
    call :UNSETVAR 
    goto End 
) else (
    echo This is a 32bit system. Installing only 32bit version.... 
    call :SETVAR32 
    call :INSTALL 
    call :UNSETVAR 
    goto End 
) 

:SETVAR64 

    set Arch=64 
    call :SETVERSION 
    set ProductRegKey={26A24AE4-039D-4CA4-87B4-2F8%Arch%1%Maj%0%Min%F0} 
    set ExeArch=x64 
    set Wow= 

GOTO:EOF 

:SETVAR32 

    set Arch=32 
    call :SETVERSION 
    set ProductRegKey={26A24AE4-039D-4CA4-87B4-2F8%Arch%1%Maj%0%Min%F0} 
    set ExeArch=i586 
    set Wow= 

GOTO:EOF 

:SETVERSION 

    SETLOCAL ENABLEDELAYEDEXPANSION 
    SET count=1 
    FOR /F "tokens=* USEBACKQ" %%F IN (`dir /B %ProductName%*`) DO (
     SET var!count!=%%F 
     SET /a count=!count!+1 
    ) 
    set Version=!var1:~4,-18! 
    set Maj=%var1:~4,-21% 
    set Min=%var1:~6,-17% 
    ENDLOCAL & set Version=%Version% & set Maj=%Maj% & set Min=%Min% 

GOTO:EOF 

:INSTALL 

    echo "--------------------------------------------------------" 
    reg query HKLM\SOFTWARE\%Wow%Microsoft\Windows\CurrentVersion\Uninstall\%ProductRegKey% /z 
    if %errorlevel%==0 (
     echo %ProductName% %Version% %Arch%bit is already installed or another error occurred... 
    ) else (
     echo %ProductName% %Version% %Arch%bit is not installed. 
     call :KILLPROC 
     call :UNINSTALLOLD 
     call :DEPLOY 
     call :CONFIGFILES 
     call :REGKEYS 
    ) 

GOTO:EOF 

:KILLPROC 

    echo "--------------------------------------------------------" 
    echo Killing browsers and unwanted running processes: 
    for %%P in (iexplore.exe firefox.exe chrome.exe jqs.exe jusched.exe jucheck.exe) do (
     cmd /c "taskkill /IM %%P /F" 
    ) 

GOTO:EOF 

:UNINSTALLOLD 

    echo "--------------------------------------------------------" 
    echo Uninstall the installed %ProductName% versions for %Arch%bit arch... 
    SETLOCAL enabledelayedexpansion 
    for /L %%A in (4,1,9) do (
     set "Maj=%%A" 
     for /L %%B in (0,1,100) do (
      set "Min=%%B" 
      set ProductRegKey={26A24AE4-039D-4CA4-87B4-2F8%Arch%1%Maj%0%Min%F0} 
      reg query HKLM\SOFTWARE\%Wow%Microsoft\Windows\CurrentVersion\Uninstall\!ProductRegKey! 
      if !errorlevel!==0 (
       echo Found the installation of !ProductName! with key !ProductRegKey!.... Uninstalling... 
       cmd /c "%Uninstall% !ProductRegKey!" 
      ) else (
       echo The installation of %ProductName% with key !ProductRegKey! not found... Skipping 
      ) 
     ) 
    ) 
    Endlocal 

GOTO:EOF 

:DEPLOY 

    echo "--------------------------------------------------------" 
    echo Deploying %ProductName% %Version% %Arch%bit 
    .\%ProductName%-%Version%-%OS%-%ExeArch%.%FileType% %Parameters% 

GOTO:EOF 

:CONFIGFILES 

    echo "--------------------------------------------------------" 
    echo Copying configuration files 
    robocopy ..\CONFIG %Windir%\ *.* /E /IS 

GOTO:EOF 

:REGKEYS 

    echo "--------------------------------------------------------" 
    REG DELETE "HKLM\SOFTWARE\%Wow%Microsoft\Windows\CurrentVersion\Run" /v SunJavaUpdateSched /f 
    for %%R in (EnableJavaUpdate NotifyDownload EnableAutoUpdate Frequency NotifyInstall UpdateSchedule EnableJavaUpdate NotifyDownload EnableAutoUpdate Frequency SunJavaUpdateSched) do (
     cmd /c "REG ADD "HKLM\SOFTWARE\%Wow%JavaSoft\Java Update\Policy" /v %%R /t REG_DWORD /d 0 /f" 
    ) 

GOTO:EOF 

:UNSETVAR 

    set ProductName= 
    set OS= 
    set FileType= 
    set LogLocation= 
    set Parameters= 
    set Uninstall= 
    set Version= 
    set Maj= 
    set Min= 

GOTO:EOF 

:End 

    echo "--------------------------------------------------------" 
    echo Installation completed 
    echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 

GOTO:EOF 

Endlocal 

的問題是與增值經銷商:

版本,少校,閔

我通過在安裝程序(i586和x64)所在的目錄中使用dir來獲得:SETVERSION函數。

這些都是空的,我不明白爲什麼,也許是因爲我在批處理結束時未設置它們?如果是,爲什麼?

非常感謝!

回答

1

不幸的是,你沒有向我們展示任何由例程產生的響應。

這種說法可能是錯誤的:

ENDLOCAL & set Version=%Version% & set Maj=%Maj% & set Min=%Min% 

應該

ENDLOCAL & set Version=%Version%& set Maj=%Maj%& set Min=%Min% 

正如你所編碼的話,之前的&的空間(S)將被納入在從setversion`返回時分配給versionmaj的值。

使用set "var=value"更安全 - 包括set字符串分配的引號。以這種方式,尾隨空格被忽略。

它更好地使用windows批處理的cmd標記。即使dos也會引起爭議,儘管它在win-batchworld中被普遍但嚴格地使用不正確,因爲它比'模擬MS-DOS版本5的應用程序在Windows-NT及其成功版本中增強的程序稍短。「

+0

Gret @Magoo !!!它的工作原理!!!謝謝你的澄清! – S4rg0n

1

​​函數列出名爲jre*的文件夾,並從首先找到的名稱中挑選出JRE版本。我猜你已將文件夾重命名爲其他內容,或者您​​正在從其他位置運行該腳本。

測試 - 在運行腳本之前,運行dir jre*並查看顯示內容。對於這個工作,你需要看到的財產以後,如:

jre1.8.0.66 
jre1.7.0.91 
... 

如果你什麼也看不到或其他一些文件命名結構,那麼這是你的問題。

+0

謝謝@OldCurmudgeon!問題是其他一些,文件結構ii如預期的那樣:dir/B jre * jre-8u66-windows-i586.exe jre-8u66-windows-x64.exe – S4rg0n