How to create a batch file which work for both Program Files and Program Files(x86)?

你说的曾经没有我的故事 提交于 2019-11-30 01:29:38

问题


I have created a batch file which automatically copy a .sql file to the path of installed Jasper server(it could be any software installation directory).

This is my batch script--

C:\PROGRA~2\JASPER~1.0\mysql\bin\mysql.exe -u root -proot < create_database.sql

that is working when jasper is installed in Program Files(x86). How can i generalize it for both Program Files and Program Files(x86).


回答1:


You want to use environment variables to look up things like this. On 32bit Windows, %ProgramFiles% is your friend.

On 64bit Windows, things are a little more complicated, as application can be installed both in %ProgramFiles% and %ProgramFiles(x86)%.

If you cannot look up the location of Jasper some other way (registry? installed program settings?), your best bet is to look in both places and take the one where you find the expected directory.

Edit Saw the nsis tag - are you creating an installer? In that case the constant $ProgramFiles might be useful.




回答2:


Here's one way of doing it, which I've copied from this source: http://social.msdn.microsoft.com/Forums/zh/winforms/thread/69dc2aac-9956-40a0-9826-da48b9003a8e

SET ProgFiles86Root=%ProgramFiles(x86)%
IF NOT "%ProgFiles86Root%"=="" GOTO win64
SET ProgFiles86Root=%ProgramFiles%
:win64

"%ProgFiles86Root%\name of program" "arguments, etc."



回答3:


In NSIS you can generally just pretend that x64 does not exist and just use $programfiles

In a batch file; if %ProgramFiles(x86)% is defined then you can assume that you are on a 64 bit system and that %ProgramFiles(x86)% is the 32 bit folder and %ProgramFiles% is the 64 bit folder. You can also check PROCESSOR_*: PROCESSOR_ARCHITEW6432 is defined for a 32 bit batch file running on a 64 bit system. PROCESSOR_ARCHITECTURE is AMD64 for a x86-64/AMD64 bit batch file (Remember that PROCESSOR_ARCHITECTURE is not just x86 or AMD64, there is also IA64 and for NT4 even more values)

You should also keep in mind that the variables can be changed by the user or might not exist at all so if you are generating the batch with NSIS it is probably better to use the full paths NSIS gives you...




回答4:


how about something simple like,

if exist "C:\Program Files (x86)" goto 64bit

goto 32bit

:32bit

(whatever u want to happen for the 32bit system)

:64bit

(whatever u want to happen for the 64bit system)

i have a script set up like this and works perfectly for both systems.

sorry for double spacing it didn't want to keep the format correct.




回答5:


Here is how I do it:

GOTO %PROCESSOR_ARCHITECTURE%

:AMD64
<64Bit code>
EXIT

:X86
<32bit code>
EXIT



回答6:


Seems like @RenniePet answer is good. For an alternative here is the way I did it. None too efficient and cribbed together from the answers here, mainly from @Samuel's answer. With this solution only the directory structure is being relied upon: there is no reference environment variables.

@echo off
dir "C:\Program Files (x86)\\." > NUL 2>&1
if errorlevel 0 (
    set PROGRAMS_HOME="C:\Program Files (x86)"
    GOTO HomeSet
)
dir "C:\Program Files\\." > NUL 2>&1
if errorlevel 0 (
    set PROGRAMS_HOME="C:\Program Files"
    GOTO HomeSet
)   
GOTO NotWindows
:HomeSet
set PROGRAMS_HOME=%PROGRAMS_HOME:"=%
echo PROGRAMS_HOME set to *%PROGRAMS_HOME%*
:NotWindows


来源:https://stackoverflow.com/questions/10071300/how-to-create-a-batch-file-which-work-for-both-program-files-and-program-filesx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!