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

发布于 2024-09-02 06:02:32 字数 277 浏览 2 评论 0原文

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

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

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

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

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:

"%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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朦胧时间 2024-09-09 06:02:32

你可以检查它是否存在&存储路径;

@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

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
只是偏爱你 2024-09-09 06:02:32

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  

VBS script:

 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  
洛阳烟雨空心柳 2024-09-09 06:02:32

“whereis”是一个Linux命令来做到这一点,但Windows端口是可用的(unxutils,我相信)。没有它,你就无法找到路径。

"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.

香草可樂 2024-09-09 06:02:32

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

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 Object for most part.

童话里做英雄 2024-09-09 06:02:32

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文