How do I set the value of multiple variables in a macro in batch?

429 Views Asked by At

I am writing a script template to install multiple MSI's, including error logging and a rollback function. It contains scriptblocks to store install and uninstall information of each MSI.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:: Global Variables
SET PACKAGENAME=SysinternalsSuite
SET THISDIR=%~dp0
SET LOGFOLDER=%TEMP%\%PACKAGENAME%
IF EXIST "%LOGFOLDER%" RD "%LOGFOLDER%" /S /Q
MKDIR "%LOGFOLDER%"
SET LOGLEVEL=/lv

:: Scriptblock
SET /A INDEX=0
SET DISPLAYNAME[%I%]=SysinternalsSuite.msi
SET MSIPATH[%I%]="%thisdir%SysinternalsSuite.msi"
SET GUID[%I%]={79D7C0BA-9E50-44E8-8D0B-56D5EA692B5E}
SET ACTIONLOG[%I%]="%LOGFOLDER%\%I%_!DISPLAYNAME[%I%]!.log"
SET INSTALLCOMMAND[%I%]=MsiExec.exe /i !MSIPATH[%I%]! /qn %LOGLEVEL% !ACTIONLOG[%I%]!
SET UNINSTALLCOMMAND[%I%]=MsiExec.exe /x !GUID[%I%]! /qn
SET EXITATERROR[%I%]=1
SET ALLOWEDERROR[%I%]=3010

ECHO !INSTALLCOMMAND[%I%]!
ECHO !UNINSTALLCOMMAND[%I%]!

The idea is, that only the DISPLAYNAME, MSIPATH and GUID need to be defined, and the last 5 lines of the block will be part of a template. Is there a way I can use a marco for the last 5 lines of the scriptblock? I've tried with DOSKEY, but that doesn't seem to work in batch, and with all the percent signs and exclamation marks, it gets tricky. Any suggestions will be appreciated!

EDIT: Added some variables and ECHO commands, to illustrate what the output should be; valid MsiExec commandlines.

1

There are 1 best solutions below

0
On

I feel like such a n00b, but I basically found out how to eliminate the five lines of code, by the scriptblock to:

:: Scriptblock
SET /A INDEX=0
SET DISPLAYNAME[%I%]=SysinternalsSuite.msi
SET MSIPATH[%I%]="%thisdir%SysinternalsSuite.msi"
SET GUID[%I%]={79D7C0BA-9E50-44E8-8D0B-56D5EA692B5E}
Call :BuildCommandLines

And adding a function:

:BuildCommandLines
    SET ACTIONLOG[%I%]="%LOGFOLDER%\%I%_!DISPLAYNAME[%I%]!.log"
    SET INSTALLCOMMAND[%I%]=MsiExec.exe /i !MSIPATH[%I%]! /qn %LOGLEVEL% !ACTIONLOG[%I%]!
    SET UNINSTALLCOMMAND[%I%]=MsiExec.exe /x !GUID[%I%]! /qn
    SET EXITATERROR[%I%]=1
    SET ALLOWEDERROR[%I%]=3010
goto :eof