开发者

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

开发者 https://www.devze.com 2022-12-31 12:34 出处:网络
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

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开发者_Go百科 the script universal. Is there any way of determining that path universally?


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


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  


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消