如何在 windows .bat 脚本中检查 32bit Program Files 文件夹的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2864575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:29:05  来源:igfitidea点击:

How to check location of 32bit Program Files folder in windows .bat script

windowsfilelocationbatch-file

提问by Arek

I want to write .bat script which works under all flavours of Windows, no matter if 32 or 64 bit.

我想编写 .bat 脚本,它可以在所有版本的 Windows 下运行,无论是 32 位还是 64 位。

In this script I want to run some file.exe. That file is located in C:\Program Files\ under 32-bit systems or C:\Program FIles (x86)\ under x64 systems. I can write:

在这个脚本中,我想运行一些 file.exe。该文件位于 32 位系统下的 C:\Program Files\ 或 x64 系统下的 C:\Program FIles (x86)\。我可以写:

"%ProgramFiles(x86)%\file.exe" under 64bit systems or "%ProgramFiles%\file.exe" under 32bit systems but I want to make the script universal. Is there any way of determining that path universally?

64位系统下的“%ProgramFiles(x86)%\file.exe”或32位系统下的“%ProgramFiles%\file.exe”但我想让脚本通用。有什么方法可以普遍确定该路径吗?

回答by Alex K.

You could just check for its existence & store the path;

您可以检查它是否存在并存储路径;

@echo off & setLocal enabledelayedexpansion
if exist "C:\Program Files\app1.exe" (
 set PPATH="C:\Program Files\"
) else (
 set PPATH="C:\Program Files(x86)\"
)

start "" %PPATH%app1.exe
start "" %PPATH%app2.exe
start "" %PPATH%app3.exe

回答by Prashant

VBS script:

VBS 脚本:

 set wshShell = createObject("WScript.Shell")  
    ''-----------------------------------    
    ''Get 32-bit program files path  
    ''-----------------  
    OsType = wshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")    
    If OsType = "x86" then  
            '''wscript.echo "Windows 32bit system detected"  
            strProgramFiles = wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")  
    elseif OsType = "AMD64" then  
            '''wscript.echo "Windows 64bit system detected"  
            strProgramFiles = wshShell.ExpandEnvironmentStrings("%PROGRAMFILES(x86)%")  
    end if  

回答by Daniel

"whereis" is a linux command to do that, but a windows port is available (unxutils, I believe). You don't get the path without it.

“whereis”是一个用于执行此操作的 linux 命令,但可以使用 Windows 端口(我相信是 unxutils)。没有它,你就无法找到路径。

回答by Salman A

I think instead of a "bat" file you should use a VBScript/JScript file. Either of these scripts can be executed by Windows Script interpreter (wscript.exe/cscript.exe). The scripting environment and the interpreters are available on all flavors of windows so there is nothing to worry about. You can find code samples for traversing directory structure, checking file presence etc using VBScript. You can use the FileSystemObject Objectfor most part.

我认为您应该使用 VBScript/JScript 文件而不是“bat”文件。这些脚本中的任何一个都可以由 Windows 脚本解释器 (wscript.exe/cscript.exe) 执行。脚本环境和解释器可用于所有类型的窗口,因此无需担心。您可以找到使用 VBScript 遍历目录结构、检查文件存在等的代码示例。您可以在大多数情况下使用FileSystemObject 对象

回答by Milton Waddams

For simplicity's sake, you could always do the following:

为简单起见,您始终可以执行以下操作:

cd %PROGRAMFILES%
cd %PROGRAMFILES(x86)%
Program.exe

This assumes you don't have to do anything complicated, but if setting the current directory is sufficient, this should work fine.

这假设您不必做任何复杂的事情,但如果设置当前目录就足够了,这应该可以正常工作。