batch-filecmd

How do you pass a variable that contains (inner) quotes into a function/label?


@echo off
setlocal enabledelayedexpansion

set "MYFLAGS=/q:a /c:"2005_8~1.EXE /q:a /c:""msiexec /i vcredist.msi /qn""""

echo %MYFLAGS%
REM OUTPUT: /q:a /c:"2005_8~1.EXE /q:a /c:""msiexec /i vcredist.msi /qn"""
REM Good output-----------------------------------------------------------

call :InstallProgram "vc_redist.exe" "%MYFLAGS%"
goto :eof

:InstallProgram
set "PROGRAM_FLAGS=%~2"

echo %PROGRAM_FLAGS%
REM OUTPUT: /q:a /c:"2005_8~1.EXE
REM Truncated output-------------^

goto :eof

Whether I define MYFLAGS inside or outside the call statement produces the same result, the echo in the :InstallProgram function stops prematurely.

Any ideas? There has to be a batch genius out there somewhere.

enabledelayedexpansion is required in other parts of my script.

The end goal, because I know someone will ask, is a totally silent install with no reboot required prompt to the user.


Solution

  • Double up all quotes in your string. Once you're inside of :InstallProgram, convert the doubled double quotes back into single double quotes.

    @echo off
    setlocal enabledelayedexpansion
    
    set "MYFLAGS=/q:a /c:""2005_8~1.EXE /q:a /c:""""msiexec /i vcredist.msi /qn"""""""
    
    echo %MYFLAGS%
    
    call :InstallProgram "vc_redist.exe" "%MYFLAGS%"
    goto :eof
    
    :InstallProgram
    set "PROGRAM_FLAGS=%~2"
    set "PROGRAM_FLAGS=%PROGRAM_FLAGS:""="%"
    
    echo %PROGRAM_FLAGS%
    
    goto :eof