Remove Quotes from a string

A routine that will reliably remove quotes from the contents of a variable .
This routine will only affects items that both begin AND end with a double quote.

e.g. will turn "C:\Program Files\somefile.txt" into C:\Program Files\somefile.txt
while still preserving cases such as Height=5'6" and Symbols="!@#

:DeQuote

SET _DeQuoteVar=%1
CALL SET _DeQuoteString=%%!_DeQuoteVar!%%
IF [!_DeQuoteString:~0^,1!]==[^"] (
IF [!_DeQuoteString:~-1!]==[^"] (
SET _DeQuoteString=!_DeQuoteString:~1,-1! ) ELSE (GOTO :EOF) ) ELSE (GOTO :EOF)
SET !_DeQuoteVar!=!_DeQuoteString! SET _DeQuoteVar= SET _DeQuoteString= GOTO :EOF

Example

SetLocal EnableDelayedExpansion
set _MyVariable = "C:\Program Files\ss64\"
CALL :dequote _MyVariable
echo %_MyVariable%

This script is tested on Windows XP and 2003, an earlier version of the script is suitable for NT4 /Win 2000.
See the discussion thread for more detail.

"Sir, your manuscript is both good and original; the trouble is that the good parts are not original, and the original parts are not good" - Samuel Johnson

Related:

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



Back to the Top

Simon Sheppard
SS64.com