1

I have a batch script, I think my issue has to do with parentheses but I'm not sure how to work around it

This is it

REM  Detect if the OS is x86 or x64
IF DEFINED PROGRAMFILES(x86) (
  REM x64
  SET "_PROGRAMFILES=%PROGRAMFILES(x86)%"
  GOTO MAIN_PROGRAM
) ELSE (
  REM x86
  SET "_PROGRAMFILES=%PROGRAMFILES%"
  GOTO MAIN_PROGRAM
)
:MAIN_PROGRAM
SET "MY_EXE=%_PROGRAMFILES%\MYFOLDER\MYEXE.exe"
IF EXIST "%MY_EXE%" (
    ECHO YES
) ELSE (
    ECHO NO
)

Run this on x86 and its ok as the path to check is C:\Program Files\MYFOLDER\MYEXE.exe

Run this on x64 and it bombs out "not expected at this time"

I'm pretty sure its got to do with the Path C:\Program Files (x86)\

How can I work around the (x86) is thats the issue


NEW

    REM Is the OS x86

    IF "%PROCESSOR_ARCHITECTURE%" == "x86" (

      SET "_PROGRAMFILES=%PROGRAMFILES%"
      SET "_ARCH=x86"  

      ECHO CPU Architecture is: "%_ARCH%"
      ECHO Program Files Directory is: "%_PROGRAMFILES%"

      GOTO MAIN_PROGRAM

    ) 

    REM Is the OS AMD64

    IF "%PROCESSOR_ARCHITECTURE%" == "AMD64" (

      SET "_PROGRAMFILES=%PROGRAMFILES(x86)%"
      SET "_ARCH=x86"

      ECHO CPU Architecture is: "%_ARCH%"
      ECHO Program Files Directory is: "%_PROGRAMFILES%"

      GOTO MAIN_PROGRAM

    )


    GOTO ERROR_OS_ARCH

    :ERROR_OS_ARCH

    CLS

    cWnd.exe /SHOW @

    ECHO.
    ECHO Error 100: Operating System Architecture not Supported. Contact your IT Department
    ECHO.

    EXIT /B
4
  • by the way the exact error is "\MYFOLDER\MYEXE.exe" is not expected at this time, indicating the variable %_PROGRAMFILES% is blank? I added an "ECHO %_PROGRAMFILES% above the IF statement and it displays correctly. C:\Program Files (x86) Commented Mar 20, 2012 at 9:41
  • 1
    Works fine for me on Vista x64. Everything looks good, with appropriate quoting, etc. Is the posted code the exact code or an excerpt that might not behave the same as the original? Commented Mar 20, 2012 at 12:26
  • Seconding that, worked for me on Windows 7 SP1, also on Windows XP SP3. Commented Mar 20, 2012 at 15:06
  • it is just an exert from a larger script, I will do some more testing, and paste the full thing if I'm still stuck, Cheers. Commented Mar 21, 2012 at 3:44

2 Answers 2

1

I've had some issues with the else operator... you could just do it with two if statements.

IF DEFINED PROGRAMFILES(x86) (
  REM x64
  SET "_PROGRAMFILES=%PROGRAMFILES(x86)%"
  GOTO MAIN_PROGRAM
)
IF NOT DEFINED PROGRAMFILES(x86) (
  REM x86
  SET "_PROGRAMFILES=%PROGRAMFILES%"
  GOTO MAIN_PROGRAM
)

Or it could also be that you use () in a variable name: PROGRAMFILES(x86) and batch is getting confused. maybe try changing that variable name to PROGRAMFILES_x86. Just kinda spitballing here.

Sign up to request clarification or add additional context in comments.

2 Comments

I have added some additional info to the first post using your idea of splitting the IF Statements.
I think its sorted. appreciate the help guys
1

This is pretty simple I think...

How to detect the OS:

FOR /F "tokens=1 skip=1" %%a IN ( 'wmic os get OSArchitecture' ) DO (IF /I '%%a'== '64-bit' (set osArch=64) ELSE (IF /I '%%a'== '32-bit' (set osArch=32)))

Next getting to the correct Program Files Folder:

FOR /F "tokens=1,2 delims==" %%a IN ( '^set "ProgramFiles"' ) DO set _PROGRAMFILES=%%a

Realistically you just need to aforementioned line.

Try this out...

FOR /F "tokens=1,2 delims==" %%a IN ( 
'^set "ProgramFiles"' 
) DO set _PROGRAMFILES=%%a
GOTO MAIN_PROGRAM

:MAIN_PROGRAM
SET "MY_EXE=%_PROGRAMFILES%\MYFOLDER\MYEXE.exe"
IF EXIST "%MY_EXE%" (
    ECHO YES
) ELSE (
    ECHO NO
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.