0

我应该如何重新编码包含以下命令的 Windows XP cmd 批处理文件,以便在两个 .exe可能位于Program Files (x86)中的更高版本的 Windows 上也能正常工作?

start "GW" /WAIT "%PROGRAMFILES%\GoldWave\GoldWave.exe" [...arguments...]
start "BC" /WAIT "%PROGRAMFILES%\Beyond Compare 3\BCompare.exe" [...arguments...]

这失败了:

PATH=%PATH%;%PROGRAMFILES%;%PROGRAMFILES(X86)%
...
start "GW" /WAIT "GoldWave\GoldWave.exe" [...arguments...]
start "BC" /WAIT "Beyond Compare 3\BCompare.exe" [...arguments...]

这成功但很笨拙:

PATH=%PATH%;%PROGRAMFILES%\Goldwave;%PROGRAMFILES(X86)%\Goldwave;%PROGRAMFILES%\Beyond Compare 3;%PROGRAMFILES(X86)%\Beyond Compare 3%
...
start "GW" /WAIT "GoldWave.exe" [...arguments...]
start "BC" /WAIT "BCompare.exe" [...arguments...]
4

1 回答 1

3

您可以先检查是否%ProgramFiles(x86)%有值。如果是这样,您正在寻找的程序应该在该目录中找到:

SETLOCAL
SET PROGRAMFILESINUSE=%ProgramFiles%
IF NOT "%ProgramFiles(x86)%"=="" SET PROGRAMFILESINUSE=%ProgramFiles(x86)%
START "GW" /WAIT "%PROGRAMFILESINUSE%\GoldWave\GoldWave.exe"

或者,您可以让它更复杂一些,并让它实际检查您要查找的文件存在的位置:

SETLOCAL
SET PROGRAMDIRECTORY=
SET RELATIVEFILEPATH=GoldWave\Goldwave.exe
CALL :CHECKFORPROGRAM "%RELATIVEFILEPATH%" "%ProgramFiles(x86)%"
CALL :CHECKFORPROGRAM "%RELATIVEFILEPATH%" "%ProgramFiles%"
IF "%PROGRAMDIRECTORY%"=="" GOTO :NOTFOUND
ECHO Found at %PROGRAMDIRECTORY%\%RELATIVEFILEPATH%
GOTO :EOF
:CHECKFORPROGRAM
IF NOT "%PROGRAMDIRECTORY%"=="" GOTO :EOF
IF EXIST "%~2\%~1" SET PROGRAMDIRECTORY=%~2
GOTO :EOF
:NOTFOUND
ECHO %RELATIVEFILEPATH% not found
GOTO :EOF
于 2013-01-15T23:48:39.500 回答