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

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Sharad
  • 3,562
  • 6
  • 37
  • 59
  • What about "Archivos de programa"? – Álvaro González Apr 09 '12 at 09:28
  • C:\PROGRA~2 could be any folder, you don't know anything about the 8.3 name generation on the end users machine... – Anders Apr 09 '12 at 09:33
  • 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.. – kodybrown Aug 16 '13 at 08:56

6 Answers6

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."
RenniePet
  • 11,420
  • 7
  • 80
  • 106
  • 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. – Travis Oct 21 '14 at 20:14
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.

Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
  • Yes i am creating an installer using NSIS script, How can i write above script for NSIS. – Sharad Apr 09 '12 at 12:12
  • 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. – Sharad Apr 10 '12 at 05:28
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...

Anders
  • 97,548
  • 12
  • 110
  • 164
  • How can i solve the UAC problem because the exe which i have created works on some system not for all system? – Sharad Apr 09 '12 at 11:19
  • Actually the installer which i have created, is not working for all systems. It gives permission error. – Sharad Apr 09 '12 at 17:52
  • 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. – Sharad Apr 13 '12 at 06:33
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.

1

Here is how I do it:

GOTO %PROCESSOR_ARCHITECTURE%

:AMD64
<64Bit code>
EXIT

:X86
<32bit code>
EXIT
Adam
  • 11
  • 1
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
Community
  • 1
  • 1
Chris Murphy
  • 6,411
  • 1
  • 24
  • 42