0

I'm trying to create a little batch file in order to silently install Firefox.

I would like to store the executable file name into a variable (SETUP_64 or SETUP_32). This would allow me to change the file version without edditing the script.

@echo off
SETLOCAL

set SETUP_64="Firefox-*-64.exe"
set SETUP_32="Firefox-*-32.exe"

if %processor_architecture% == AMD64 (
  start %SETUP_64%

) else (
  start %SETUP_32%
)


Echo DONE

PAUSE
ENDLOCAL

Problem is that the script is not working properly. Can anyone help me find the solution?

Is there another command than start %variable%? Maybe my variable is wrong?

Executable files are stored at the same place as my script install.bat.

Compo
  • 36,585
  • 5
  • 27
  • 39
Jack WOLF
  • 3
  • 2
  • 1
    The `START` command treats the first quoted argument as the Window Title. So you need an empty set of quotes. `start "" %SETUP_64%`. Best practice is to NOT assign quotes to variables but do you use them to protect special characters and from trailing spaces being assigned to a variable. `set "SETUP_64=Firefox-*-64.exe"` – Squashman Apr 04 '19 at 15:04
  • @Squashman Thank you very much for your answer. It's seems to be working. The only problem is that my script as trouble to find "Firefox-*-64.exe". But it's working if I put the full name inside the variable (without *). Any idea? – Jack WOLF Apr 04 '19 at 15:09
  • @Compo Does it mean that I have to rename my file `Firefox-*-AMD64.exe` ? What is `:~-2` – Jack WOLF Apr 04 '19 at 15:14
  • No, `%PROCESSOR_ARCHITECTURE:~-2%` holds only the last two characters of `%PROCESSOR_ARCHITECTURE%`. – Compo Apr 04 '19 at 15:15
  • Sorry, I missed the asterisk in your code. I just saw the obvious problems with your code. Why do you not know the complete file name? – Squashman Apr 04 '19 at 15:29
  • 1
    Don't use the environment variable `PROCESSOR_ARCHITECTURE`. It's value is `x86` when the batch file is executed by 32 bit `cmd.exe` because of application started the batch file is also a 32 bit application as documented by Microsoft in article about [WOW64 Implementation Details](https://docs.microsoft.com/en-us/windows/desktop/WinProg64/wow64-implementation-details). I recommend something like `if "%ProgramFiles(x86)%" == "" (set "FireFoxArchitecture=32") else set "FireFoxArchitecture=64"`. Double click on `C:\Windows\SysWOW64\cmd.exe` and run `set proc` and you see that I'm right. – Mofi Apr 04 '19 at 19:30
  • 1
    And please first verify if Firefox is not already installed before installing Firefox and also its version before installing an older version over an already installed newer version. See for example [Silently Updating Firefox via Command Prompt (Windows)](https://stackoverflow.com/questions/29768157/). – Mofi Apr 04 '19 at 19:33

2 Answers2

1

Here's an idea for performing the task, which assumes that you're happy to run the executable regardless of whether an existing version of Firefox is already installed or not.

@Echo Off
If Defined PROCESSOR_ARCHITEW6432 (
    Start "" /D "%__CD__%" "%SystemRoot%\SysNative\cmd.exe" /C "%~f0"&Exit /B)
If %PROCESSOR_ARCHITECTURE:~-2% Equ 86 (Call :Sub 32)Else Call :Sub 64
Pause
GoTo :EOF

:Sub
For %%A In ("%~dp0Firefox-*-%1.exe")Do (Start "" /Wait "%%A"
    If Not ErrorLevel 1 Echo Done)
Exit /B

This works by ensuring that the appropriate version of cmd.exe is running the batch file first. This is necessary because if the 32-bit version of cmd.exe is running the script on a x64 system, the %PROCESSOR_ARCHITECTURE% variable value will be x86. However when the 32-bit version of cmd.exe is running on a x64 system, a new environment variable %PROCESSOR_ARCHITEW6432% is defined, which does not exist in 64-bit cmd.exe on a x64 system, or 32-bit cmd.exe on a x86 system. Once the script is running in the correct bitness version of cmd.exe, we can rely on the output of %PROCESSOR_ARCHITECTURE% for running the appropriate executable.

As an additional comment, the Firefox executables, which are essentially just self-extracting 7z archives, appear to have exactly the same name, regardless of which bitness version you download, e.g. Firefox Setup 66.0.2.exe. This means that the executables in the question must have been been renamed, and so you could rename them to anything at all you want. If you use the same renaming scheme, but instead use -86 instead of -32 you could simplify the code a little too:

@Echo Off
If Defined PROCESSOR_ARCHITEW6432 (
    Start "" /D "%__CD__%" "%SystemRoot%\SysNative\cmd.exe" /C "%~f0"&Exit /B)
For %%A In ("%~dp0Firefox-*-%PROCESSOR_ARCHITECTURE:~-2%.exe")Do (
    Start "" /Wait "%%A"
    If Not ErrorLevel 1 Echo Done
    Pause)
Compo
  • 36,585
  • 5
  • 27
  • 39
0

I moved a quote, and I added the silent install flag.

@echo off
SETLOCAL

set "SETUP_64=Firefox*64.exe"
set "SETUP_32=Firefox*32.exe" 

if %processor_architecture% == AMD64 (
  start %SETUP_64% -ms

) else (
  start %SETUP_32% -ms
)


Echo DONE

PAUSE
ENDLOCAL
shadow2020
  • 1,315
  • 1
  • 8
  • 30