DOS 子串和变量

DOS substrings and variables

我想用来安装的 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

问题出在变量上:

版本、主要、最小

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

它们是空的,我不明白为什么,可能是因为我在批处理结束时取消了它们?如果是,为什么?

非常感谢!!

:SETVERSION 函数列出名为 jre* 的文件夹并从第一个找到的名称中挑选出 JRE 版本。我猜你已经将文件夹重命名为其他文件夹,或者你正在 运行 从不同的位置安装脚本。

测试 - 在 运行 脚本之前,运行 dir jre* 看看显示了什么。为此,您需要看到类似以下内容:

jre1.8.0.66
jre1.7.0.91
...

如果您看不到任何文件或其他文件命名结构,那就是您的问题。

遗憾的是,您没有向我们展示例程生成的任何响应。

这个说法可能有误:

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

应该是

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

如您所编码,& 之前的 space(s) 将包含在分配给 version 和 [= 的值中13=] 在 return 来自 setversion`。

使用 set "var=value" 更安全 - 包括 set 字符串赋值的引号。这样,后面的 space 将被忽略。

windows 批处理最好使用 cmd 标签。即使 dos 也会受到攻击,尽管它在 win-batchworld 中被普遍但严格错误地使用,因为它比“模拟 MS-DOS 版本 5 的应用程序在 Windows-NT 中得到增强并且它的继任者。"