DOS子串和变量

时间:2015-11-25 11:03:40

标签: java function cmd batch-processing

我有一个DOS批处理问题,我想用它来安装java:

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

问题在于vars:

版本,Maj,Min

我通过在安装程序(i586和x64)所在的目录中使用dir进入:SETVERSION函数。

是空的,我不明白为什么,也许是因为我在批次结束时取消了它们?如果是,为什么?

非常感谢!!

2 个答案:

答案 0 :(得分:1)

:SETVERSION函数列出了名为jre*的文件夹,并从首先找到的名称中挑选出JRE版本。我猜你已经将文件夹重命名为其他东西,或者你从不同的位置运行脚本。

要测试 - 在运行脚本之前,请运行dir jre*并查看显示的内容。要做到这一点,你需要看到像:

这样的东西
jre1.8.0.66
jre1.7.0.91
...

如果您没有看到任何内容或其他文件命名结构,那么这就是您的问题。

答案 1 :(得分:1)

可悲的是,您没有向我们展示例程生成的任何回复。

这句话可能有误:

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

应该是

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

正如您编码的那样,& 之前的空格将包含在从setversion返回时分配给versionmaj的值中

使用set "var=value"更安全 - 包括set字符串分配的引号。这样,尾随空格将被忽略。

最好使用Windows批处理的cmd标记。即使dos在win-batchworld中通常但严格错误地使用它也会引起攻击,因为它比在Windows-NT及其成功者中增强模拟MS-DOS版本5的应用程序要短一些。“