找到程序是否存在,并在批处理窗口中设置自定义变量 [英] Locate if the program exists and set a custom variable in batch windows

查看:49
本文介绍了找到程序是否存在,并在批处理窗口中设置自定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到程序是否存在,并在批处理窗口中设置自定义变量.

Locate if the program exists and set a custom variable in batch windows.

我想知道是否安装了特定程序并知道其位置.问题是有两个未知数.第一个是x86和x64之间的平台.第二个是在安装路径中定义的版本号.对此添加了一个约束,如果存在多个版本,请选择最新的首选项x64平台.

I wonder if a specific program is installed and know its location. The problem is that there are two unknowns. The first is the platform between x86 and x64. The second is the version numbers defined in the installation path. A constraint is added to this, if multiple versions exist, choose the latest and preference x64 platform.

以GhostScript和ImageMagick为例,可能安装了多个版本:

GhostScript and ImageMagick example, there may be more than one version installed:

C:\Program Files\gs\gs9.16\bin
C:\Program Files (x86)\gs\gs9.15\bin
C:\Program Files\ImageMagick-6.9.2-Q16
C:\Program Files\ImageMagick-6.9.2-Q8

我有一个曲目,但不是确定性的.

I have a track but it is not conclusive.

@ECHO OFF    
@SETLOCAL enableexpansion
@FOR /F "tokens=*" %%G IN ('@dir/b /s /a:d "%ProgramFiles(x86)%\gs\gs*" "%ProgramFiles%\gs\gs*" "%ProgramFiles%\doesnt-exist*"') DO @SET _GSWIN="%%G\bin\gswin.exe"
@echo %_GSWIN%
ENDLOCAL

推荐答案

在命令提示符窗口中运行set列出当前用户的所有环境变量.

Running in a command prompt window set lists all environment variables for current user.

所有用户帐户和用户变量都存在系统变量,默认情况下,系统控制面板中仅为用户自定义定义了2个用户变量:TEMPTMP替换了系统帐户的系统变量TEMPTMP. Windows会自动定义其他与用户帐户相关的变量,例如USERPROFILEUSERNAME等.

There are system variables which exist for all user accounts and user variables whereby there are by default only 2 user variables defined for user customization in System Control Panel: TEMP and TMP which replace the system variables TEMP and TMP of system account. The other user account related variables like USERPROFILE, USERNAME, etc. are defined by Windows automatically.

在Windows x86上只有变量ProgramFiles,而在Windows x64上只有变量ProgramFilesProgramFiles(x86).

On Windows x86 there is only the variable ProgramFiles while on Windows x64 there are the variables ProgramFiles and ProgramFiles(x86).

因此,从下面的批处理代码所示,找出运行Windows x86或x64的计算机上是否执行了批处理文件的最简便,最佳的方法是分别检查环境变量ProgramFiles(x86)的值.

So the easiest and in my point of view best method to find out if batch file is executed on a machine running Windows x86 or x64 is checking existence respectively value of environment variable ProgramFiles(x86) as the batch code below demonstrates.

我几乎从不建议偶然在目录中搜索可执行文件.大多数安装程序使用户有机会将应用程序安装到任何目录中.因此,在%ProgramFiles%%ProgramFiles(x86)%中搜索应用程序目录或可执行文件通常不是一个好主意.

I nearly never recommend searching for an executable in directories by chance. Most installers give the user the opportunity to install an application into any directory. Therefore searching for an application directory or executable in %ProgramFiles% and %ProgramFiles(x86)% is in general not a good idea.

大多数应用程序安装程序会向注册表添加一些内容,其中包含已安装应用程序的版本和目录.原因很简单.如果已经安装了以前的版本,安装的版本具有哪个版本以及应用程序的安装位置,则安装程序需要在更新/升级时了解自己.

Most application installers add something to registry containing version and directory of installed application. The reason is simple. The installer needs to know itself on update/upgrade if a previous version is already installed, which version the installed version has, and where the application is installed.

许多安装程序会添加一个用于卸载的注册表项,许多安装程序还会将已安装应用程序的可执行文件也添加到注册表项App Paths中.有关App Paths的详细信息,请参见

Many installers add a registry key for uninstall and many add the executable of the installed application also to registry key App Paths. For details about App Paths see

  • How to enumerate all programs that can open XML file as plain text?
  • Silently Updating Firefox via Command Prompt (Windows)
  • Where is "START" searching for executables?

我在运行德语Windows XP x86的旧计算机上仅安装了 Ghostscript 8.71. Ghostscript v8.71的安装程序尚未向App Paths添加任何可执行密钥.但是 Ghostscript 安装程序已将注册表项GPL Ghostscript添加到HKLM\Software,其中又一个子项是已安装的 Ghostscript 的版本和两个注册表值.

I have installed only Ghostscript 8.71 on an old machine running German Windows XP x86. Installer of Ghostscript v8.71 has not added any executable key to App Paths. But the Ghostscript installer has added the registry key GPL Ghostscript to HKLM\Software with one more subkey being version of installed Ghostscript and two registry values.

[HKEY_LOCAL_MACHINE\SOFTWARE\GPL Ghostscript\8.71]
"GS_DLL"="C:\\Programme\\Ghostscript\\gs8.71\\bin\\gsdll32.dll"
"GS_LIB"="C:\\Programme\\Ghostscript\\gs8.71\\lib;C:\\Programme\\Ghostscript\\fonts"

Ghostscript v8.71已安装到德语Windows XP x86的标准程序文件目录中.

Ghostscript v8.71 is installed into standard program files directory of German Windows XP x86.

因此,我建议获取GPL Ghostscript的所有子项,以确定已安装的 Ghostscript 的最高版本,以及从字符串值GS_DLL确定DLL文件的路径.

Therefore I suggest to get all subkeys of GPL Ghostscript to determine highest version of an installed Ghostscript and the path of the DLL file from string value GS_DLL.

下面的批处理代码演示了Windows标准程序文件目录的评估以及如何获取具有最高版本号的已安装 Ghostscript 的目录和版本.

The batch code below demonstrates evaluation of the standard program files directories of Windows and how to get directory and version of an installed Ghostscript with highest version number.

@echo off
setlocal EnableExtensions EnableDelayedExpansion

if "%ProgramFiles(x86)%" == "" (
    echo Windows x86 is running on this machine.
    echo.
    echo The standard program files directory is:
    echo    %ProgramFiles%
) else (
    echo Windows x64 is running on this machine.
    echo.
    echo The standard program files directories are:
    echo    %ProgramFiles%
    echo    %ProgramFiles(x86)%
)
echo.

call :GetGsData

if "%GhostscriptDirectory%" EQU "" (
    echo Ghostscript is not installed on this machine.
    goto EndBatch
)

echo Found Ghostscript version %GhostscriptVersionMajor%.%GhostscriptVersionMinor% in directory:
echo    %GhostscriptDirectory%
goto EndBatch


rem Look in registry under "HKEY_LOCAL_MACHINE\Software\GPL Ghostscript"
rem and also under "HKEY_LOCAL_MACHINE\Software\Wow6432Node\" for one or
rem more installed versions of Ghostscript and get latest version of a
rem still installed Ghostscript as well as its installation directory.

:GetGsData
set "GhostscriptVersionMajor=0"
set "GhostscriptVersionMinor=0"
set "GhostscriptDirectory="

rem call :GetGsVersion "HKCU\Software\GPL Ghostscript"
call :GetGsVersion "HKLM\Software\GPL Ghostscript"
call :GetGsVersion "HKLM\Software\Wow6432Node\GPL Ghostscript"
goto :EOF

:GetGsVersion
for /F "skip=2 tokens=4,5 delims=.\" %%I in ('%SystemRoot%\System32\reg.exe query "%~1" 2^>nul') do (
    call :GetGsDirectory "%%I" "%%J"
)
goto :EOF


rem This subroutine queries for string value GS_DLL in found Ghostscript
rem registry key. If this string value is not found, this registry key
rem is ignored and nothing is changed on the variables for Ghostscript.

rem But if the string value GS_DLL is found, it is checked next, if the
rem DLL specified with full path really exists. The subroutine is exited
rem with no change if the DLL does not exist (anymore).

rem But if the DLL exists, it is checked if the version of this installation
rem of Ghostscript is higher than a perhaps already found installation of
rem Ghostscript before. The variables for Ghostscript are update if this
rem installation is of a newer version as previous installation.

:GetGsDirectory
for /F "skip=2 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKLM\Software\GPL Ghostscript\%~1.%~2" /v GS_DLL 2^>nul') do (

    rem Is this the line with the string value GS_DLL?
    if /I "%%A" == "GS_DLL" (

        rem Does the DLL file not exist?
        if not exist "%%~C" goto :EOF

        rem Is major version lower than already determined major version?
        if %~1 LSS !GhostscriptVersionMajor! goto :EOF

        rem Is major version equal already determined major version, but
        rem minor version is lower than already determined minor version?
        if %~2 EQU !GhostscriptVersionMajor! (
            if %~2 LSS !GhostscriptVersionMinor! goto :EOF
        )

        rem Okay, this version is higher than already determined version.
        set "GhostscriptVersionMajor=%~1"
        set "GhostscriptVersionMinor=%~2"

        rem Get drive and path of DLL file.
        for %%D in ("%%~C") do set "GhostscriptDirectory=%%~dpD"
        rem Remove directory "bin" from path of the DLL file.
        set "GhostscriptDirectory=!GhostscriptDirectory:\bin\=!"
        goto :EOF
    )
)
rem Exit subroutine with no change as string value of DLL file not found.
goto :EOF


:EndBatch
endlocal
echo.
pause

最好理解 reg 输出的处理,使其在命令提示符窗口中运行以下命令一次:

It would be good for understanding the processing of the reg outputs to run once in a command prompt window the following commands:

  • reg query "HKLM\Software\GPL Ghostscript"
  • reg query "HKLM\Software\GPL Ghostscript\9.16" /v GS_DLL
  • reg query "HKLM\Software\GPL Ghostscript"
  • reg query "HKLM\Software\GPL Ghostscript\9.16" /v GS_DLL

要了解其他使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the other used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • reg query /?
  • call /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • reg query /?

如果 Ghostscript 安装程序还支持仅针对我不知道的当前用户安装 Ghostscript ,那么取消注释该行会很好

If Ghostscript installer supports also installation of Ghostscript for current user only which I don't know, it would be good to uncomment the line

rem call :GetGsVersion "HKCU\Software\GPL Ghostscript"

通过从行首删除命令 rem .

没有HKCU\Software\Wow6432Node. (或更确切地说,根据Microsoft的规范,在HKEY_CURRENT_USER\Software中应该没有Wow6432Node.)

There is no HKCU\Software\Wow6432Node. (Or more precise there should be no Wow6432Node in HKEY_CURRENT_USER\Software according to specification of Microsoft.)

我不能为 ImageMagick 提供类似的建议,因为我的任何计算机上均未安装此应用程序.我正在使用 IrfanView (私人使用免费).

I can't suggest something similar for ImageMagick as I don't have this application installed on any of my computers. I'm using IrfanView (free for private usage).

这篇关于找到程序是否存在,并在批处理窗口中设置自定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆