Remove Quotes from a string

Several routines that can be used to remove quotes from the contents of a variable.

In the majority of cases running any current version of Windows, the simplest method is to use %~1 Parameter Extensions to remove the quotes automatically

   @ECHO OFF
   Set _string=%~1
   Echo the string passed to this script is %_string% 

(you may also want to test IF the variable is empty/NULL)

This is equivalent to the following, which can be used to remove outer quotes from any string, not just a parameter string:

   :: Remove quotes
   SET _string=###%_string%###
   SET _string=%_string:"###=%
   SET _string=%_string:###"=%
   SET _string=%_string:###=%

If you just want to remove ALL quotes from a string, this can be done in one line with variable replace syntax:

   Set _somevariable=%_somevariable:"=%

File and folder names cannot (legally) contain quotes so the above is often all that's needed.

A one line function, using a FOR command to run parameter expansion and remove quotes (the %~A removes the quotes), this is a good approach when you need to dequote multiple strings, just call the function with each string:

::::::::: one line Dequote example ::::::::::::
@Echo Off
Setlocal
Set _mypath="C:\Program Files\ss64\"
CALL :dequote _mypath
Echo %_mypath%
Goto :eof

:DeQuote
for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
Goto :eof

The above will turn "C:\Program Files\ss64\" into C:\Program Files\ss64\

If the line in bold is saved as a single line batch file DeQuote.cmd, then it can be called from other batch scripts:

CALL DeQuote.cmd VariableName

There may be cases when you only want to affect the string if it both starts and ends with a quote and raise an error or some other action if mismatched quotes are found. Some more complex scripts to handle this can be found here in the forum.

“One look is worth a thousand words” ~ Fred R. Barnard

Related:

SetLocal - EnableDelayedExpansion
Long Filenames and NTFS - Valid characters in filenames


© Copyright SS64.com 1999-2016
Some rights reserved