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

   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 quotes from any string, not just a parameter string:

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

An alternative one line function, uses 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
Set _mypath="C:\Program Files\ss64\"
CALL :dequote _mypath
Echo %_mypath%
Goto :eof

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 are a couple of edge cases where all the methods above will fail: if the string does not both begin and end with a quote, or if the string begins with two quotes ""demo"

The more complex scripts below allow you to handle these cases, or at least handle them more gracefully. Only accepting strings that both begin and end with a double quote, will make the script more robust against erroneous user input.

 ::::::::: DeQuote for NT 4/ XP or greater ::::::::::::
 SET DeQuote.Variable=%1
 CALL Set DeQuote.Contents=%%%DeQuote.Variable%%%
 echo:%DeQuote.Contents%|FindStr/brv ""^">NUL:&&Goto :EOF
 echo:%DeQuote.Contents%|FindStr/erv ""^">NUL:&&Goto :EOF
 Set DeQuote.Contents=####%DeQuote.Contents%####
 Set DeQuote.Contents=%DeQuote.Contents:####"=%
 Set DeQuote.Contents=%DeQuote.Contents:"####=%
 Set %DeQuote.Variable%=%DeQuote.Contents%
 Set DeQuote.Variable=
 Set DeQuote.Contents=
 Goto :EOF
 :: Written by Frank P. Westlake, 2001.09.22, 2001.09.24
 :: Modified by Simon Sheppard 2002.06.09

An alternative version of DeQuote (for Windows XP or later) when used with EnableDelayedExpansion by Nicholi

::::::::: Dequote for Win XP or greater ::::::::::::

 FOR %%G IN (%*) DO (
 SET DeQuote.Variable=%%G
 CALL SET DeQuote.Contents=%%!DeQuote.Variable!%%
 IF [!DeQuote.Contents:~0^,1!]==[^"] (
 IF [!DeQuote.Contents:~-1!]==[^"] (
 SET DeQuote.Contents=!DeQuote.Contents:~1,-1!
 ) ELSE (GOTO :EOF no end quote)
 ) ELSE (GOTO :EOF no beginning quote)
 SET !DeQuote.Variable!=!DeQuote.Contents!
 SET DeQuote.Variable=
 SET DeQuote.Contents=

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


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

© Copyright 1999-2016
Some rights reserved