Remove Quotes from a string

A routine to remove quotes from the contents of a variable.

Simple version - for the majority of cases:

@Echo Off

Set _MyVariable="C:\Program Files\ss64\"
CALL :dequote _MyVariable
Echo %_MyVariable%
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

The script above does fail with an unmatched leading double quote "!@# or "C:\Program Files\ (this can happen where the input string is supplied by the user.) The more complex routine below will handle this and will only affect strings that both begin and end with a double quote.

 ::BEGIN FUNCTION::::::::::::::::::::::::::::::::::::::::
 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


 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-2015
Some rights reserved