Writing unified batch scripts

This page contains tricks for writing unified batch scripts. We will focus on handling platform detection, profile pathes (default and all users), start menu and desktop.

To simplify, a standard installation of Windows on C:\ is assumed. Most tests found on this page are included in the boiler plate install.bat available for download.

Windows version

This code detects if current platform is Windows XP or later:

        :: Detect Windows version:
        if "%ALLUSERSPROFILE%" == "C:\Documents and Settings\All Users" (
                set WINDOWS=XP
        )
        if "%ALLUSERSPROFILE%" == "C:\ProgramData" (
                set WINDOWS=7
        )

The WINDOWS variable set here will be used on other examples below.

Processor

The processor (arch) is detected by looking if the C:\ drive contains a program files folder for 32-bit programs:

        :: Detect processor architecture:
        if exist "C:\Program Files (x86)\" (
                set ARCH=64
        ) else (
                set ARCH=32
        )

The ARCH variable set here will be used on other examples below.

Program Files

Sometime it's useful to abstract the location of the program files directory. It could be used in detection of previous installed 32-bit versions of programs.

        :: Set program files locations:
        if %ARCH% == 64 (
                set "PF32=%ProgramFiles(x86)%"
                set "PF64=%ProgramFiles%"
        ) else (
                set "PF32=%ProgramFiles%"
                set "PF64=%ProgramFiles%"
        )

Notice that extended syntax (set "var=val") must be use because the values assigned might contain flow control characters that groks the parser. For example, the value of %ProgramFiles(x86)% contains a ')' character on 64-bit Windows that is evaluated when assigned, causing the else-branch to be taken.

All users

The all users profile is applied to all users logons. This code abstracts its location:

        :: Set all users profile path:
        if %WINDOWS% == XP (
                set PUBLIC=C:\Documents and Settings\All Users
        )
        if %WINDOWS% == 7 (
                set PUBLIC=C:\Users\Public
        )

Notice that the display name of folers (as shown by explorer) on Windows 7 differs from their real names (as seen by dir in cmd.exe).

Default user

The default user profile is used as a template for new logon users. This code sets application data and start menu location on Windows XP and 7:

        :: Set default user locations:
        if %WINDOWS% == XP (
                set DEFAULT=C:\Documents and Settings\Default User
                set APPDATA=C:\Documents and Settings\Default User\Application Data
                set STARTMENU=C:\Documents and Settings\Default User\Start Menu
        )
        if %WINDOWS% == 7 (
                set DEFAULT=C:\Users\Default
                set APPDATA=C:\Users\Default\AppData\Roaming
                set STARTMENU=C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu
        )

All other standard folders (like Documents or Favorites) are relative to the value of the DEFAULT variable.

Start menu

We have previous seen how to set the STARTMENU for default user. We will now set the start menu location for all users:

        :: Set start menu path for all users:
        if %WINDOWS% == XP (
                set STARTMENU=C:\Documents and Settings\All Users\Start Menu
        )
        if %WINDOWS% == 7 (
                set STARTMENU=C:\ProgramData\Microsoft\Windows\Start Menu
        )

Desktop

We use the WINDOWS variable and set the DESKTOP variable to reference all users desktop:

        :: Set all users desktop path:
        if %WINDOWS% == XP (
                set DESKTOP=C:\Documents and Settings\All Users\Desktop
        ) else (
                set DESKTOP=C:\Users\Public\Desktop
        )

One could also use set DESKTOP=%PUBLIC%\Desktop if all users location has already been detected.

Firefox

Firefox default settings is located in different places depending on Windows version. This code sets the FIREFOX variable for the default user profile:

        :: Set Firefox default user settings:
        if %WINDOWS% == XP (
                set FIREFOX=C:\Documents and Settings\Default User\Application Data\Mozilla\Firefox
        )
        if %WINDOWS% == 7 (
                set FIREFOX=C:\Users\Default\AppData\Roaming\Mozilla\Firefox
        )

Alternative use set FIREFOX=%APPDATA%\Mozilla\Firefox if default user profile has already been detected.