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

时间:2015-09-05 13:03:57

标签: windows batch-file

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

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

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

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

我有一条曲目,但不是决定性的。

@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

1 个答案:

答案 0 :(得分:1)

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

对于所有用户帐户和用户变量都存在系统变量,默认情况下,在系统控制面板中仅为用户自定义定义了2个用户变量:TEMP和{{1它取代了系统帐户的系统变量TMPTEMP。其他与用户帐户相关的变量(如TMPUSERPROFILE等)由Windows自动定义。

在Windows x86上只有变量USERNAME,而在Windows x64上有变量ProgramFilesProgramFiles

因此,在运行Windows x86或x64的计算机上执行批处理文件的最简单且最好的方法是检查环境变量ProgramFiles(x86)的值是否存在,如下面的批处理代码所示。

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

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

许多安装程序添加了一个用于卸载的注册表项,许多安装程序也将已安装应用程序的可执行文件添加到注册表项%ProgramFiles(x86)%。有关App Paths的详细信息,请参阅

我在运行德语版Windows XP x86的旧机器上只安装了 Ghostscript 8.71。 Ghostscript v8.71的安装程序尚未向App Paths添加任何可执行密钥。但 Ghostscript 安装程序已将注册表项App Paths添加到GPL Ghostscript,另外一个子项是已安装 Ghostscript 的版本和两个注册表值。

HKLM\Software

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

因此,我建议让[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 的最高版本以及来自字符串值GPL Ghostscript的DLL文件的路径。

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

GS_DLL

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

  • @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 query "HKLM\Software\GPL Ghostscript"

要了解其他使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • reg query "HKLM\Software\GPL Ghostscript\9.16" /v GS_DLL
  • call /?
  • for /?
  • goto /?
  • if /?
  • set /?

如果 Ghostscript 安装程序也支持仅为当前用户安装 Ghostscript ,我不知道,最好取消注释该行

reg query /?

从行首删除命令 rem

没有rem call :GetGsVersion "HKCU\Software\GPL Ghostscript" 。 (或者更确切地说,根据Microsoft的规范,HKCU\Software\Wow6432Node中应该没有Wow6432Node。)

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