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

6

13

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

Goldfarb answered 9/4, 2012 at 9:27 Comment(3)
What about "Archivos de programa"?Delozier
C:\PROGRA~2 could be any folder, you don't know anything about the 8.3 name generation on the end users machine...Miserable
I can't find anything to back it up at the moment, but I'm pretty sure that C:\PROGRA~1 is always going to point to the C:\Program Files folder, as well as the following.. C:\PROGRA~1 == C:\Program Files C:\PROGRA~2 == C:\Program Files (x86) C:\PROGRA~3 == C:\ProgramData C:\DOCUME~1 == C:\Documents and Settings C:\SYSTEM~1 == C:\System Volume Information.. I could be wrong though..Torhert
T
18

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.

Tarton answered 9/4, 2012 at 9:36 Comment(2)
Yes i am creating an installer using NSIS script, How can i write above script for NSIS.Goldfarb
If i use $ProgramFiles it only search for Program Files directory and if my Jasper directory is in Program Files(x86) directory it does not work.Goldfarb
Q
25

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."
Quatre answered 25/2, 2013 at 4:57 Comment(1)
I believe this would be a "more correct" answer as I was attempting to use "IF EXISTS" statements using %ProgramFiles% and %%ProgramFiles(x86)% but it would not correctly function. This solved the issue because it determined which directory was available and just executed the program with the correct file path. The other method that was accepted as the answer, however, is not "smart" enough to determine if there is a x86 or x64 system where as this one is. Thank you very much for this.Pickens
T
18

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.

Tarton answered 9/4, 2012 at 9:36 Comment(2)
Yes i am creating an installer using NSIS script, How can i write above script for NSIS.Goldfarb
If i use $ProgramFiles it only search for Program Files directory and if my Jasper directory is in Program Files(x86) directory it does not work.Goldfarb
M
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...

Miserable answered 9/4, 2012 at 9:57 Comment(3)
How can i solve the UAC problem because the exe which i have created works on some system not for all system?Goldfarb
Actually the installer which i have created, is not working for all systems. It gives permission error.Goldfarb
Yes i am using administrator account to install this installer, when i install any other software it ask for the permission whether you want to install it or not but in case of my installer it dose not ask any thing for the permission and completes the installation without installing my software.Goldfarb
A
1

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.

Alyce answered 23/9, 2014 at 9:10 Comment(0)
D
1

Here is how I do it:

GOTO %PROCESSOR_ARCHITECTURE%

:AMD64
<64Bit code>
EXIT

:X86
<32bit code>
EXIT
Dyne answered 13/5, 2015 at 19:44 Comment(1)
This returns x86 on a x64 machine if the batch file is executed from an x86 CMD, which is incorrect.Mainsail
A
0

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
Afterdeck answered 28/6, 2015 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.