Search code examples
doscmd

Anyone know how to strip quote characters from a string in DOS batch script?


Does anyone know how to strip quote characters from a string in DOS batch script? Stripping characters is easy with the string replace function but stripping quotes (or exclamation marks) is seeming to be a little tougher. Here is my test script I am trying to get working:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
echo.
set "NAME=Izomorphius"
call :append NAME " is my dear friend."
echo Message: %NAME%
echo.
goto :end
:: Functions
:append @varname @value
IF NOT "%3"=="" (
  echo Too many arguments to function.
)
set vara=%1
set stra=%2
set stra=%stra:""=%
set "%1=!%1! %2"
exit /B 0
:end
pause

Here is the output of this script:

Message: Izomorphius " is my dear friend."

Solution

  • The ~ modifier will strip enclosing quotes from a paramenter. Type HELP CALL from the command prompt for a complete list of modifiers. The same modifiers are also available for FOR variables.

    @echo off
    setlocal enableDelayedExpansion
    echo.
    set "NAME=Izomorphius"
    call :append NAME " is my dear friend."
    echo Message: %NAME%
    echo.
    goto :end
    
    :: Functions
    
    :append @varname @value
    IF NOT "%~3"=="" echo Too many arguments to function.
    set "%~1=!%~1! %~2"
    exit /B 0
    
    :end
    pause