17

I use:

FOR /F "delims=" %%G IN ("%command%") DO SET command=%%~G

to remove "" quotes from variable %command%. If command = "Shutdown /s /t 00", after this line it will be: Shutdown /s /t 00. and it works. But when command contains a string where are a equal sign (=), it remove also this caracter. Example:
before, command = "D:\temp\stinger --ADL --GO --Silent --ReportPath= D:\temp --ReportOnly --Delete --Program"
After, command= D:\temp\stinger --ADL --GO --Silent --ReportPath D:\temp --ReportOnly --Delete --Program

Look, the quotes "" are removed, but also the sign = .

So, how to remove the quotes "" without removing the equal character.

Thanks

2

3 Answers 3

42

Instead of your for loop through the command, you could just use string manipulation.

set command=%command:"=%

the values after command are "=<nul> so you're getting rid of quotation marks in the variable command. Just as an extra example, you could also do %command: =_% to replace all spaces in command with underscores.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for string manipulation. Suggestion: if defined command set "command=%command:"=%" Before using string manipulation you want to check that command is defined, because, if command does not exist then the manipulation will fail and set will make command literally equal to %command:"=%
@Phiber if you're interested - check out the dostips page on string manipulation.
"set command=%command:"=%" <--- this is required for & poison characters
Sure, this will work if you know that the command is quoted. But what if sometimes the command is quoted, and sometimes not? You could add IF tests with substrings to conditionally remove the first and last character. But there is a better way. See my answer
@foxidrive - Removing all quotes might not be acceptable. Perhaps the command has arguments that are supposed to remain quoted.
|
11

The reason your command fails is because the quotes in the string and the quotes in the IN() clause cancel each other out, so the remainder of the content is not quoted. The FOR /F parser treats an unquoted = as a token delimiter that is converted into a space. You would also have problems with poison characters like &, |, etc.

The problem is avoided by using delayed expansion. The delay state is toggled on before the FOR loop, and off within the loop so that any ! within the command are preserved.

setlocal enableDelayedExpansion
for /f "delims=" %%A in ("!command!") do endlocal & set "command=%%~A"

The major advantage of this approach is that you get the correct result regardless whether the command starts out quoted or not.

1 Comment

I was wondering why this wasn't working. Needed to "setlocal enabledelayedexpansion"
0

While unclemeat's answer is the best option for this particular use-case, there is an alternative that someone with a different use-case might want to explore: Using usebackq and outputting the variable through a command that outputs a string:

SET "command="D:\temp\stinger --ADL --GO --Silent --ReportPath= D:\temp --ReportOnly --Delete --Program""

<NUL: SET/P="Before: %command%" &ECHO;

FOR /F "usebackq delims=" %%G IN (`cmd/S/C "<NUL: SET/P="%command%""`) DO SET "command=%%~G"

<NUL: SET/P="After: %command%" &ECHO;

Note that <NUL: SET/P="..." (or SET/P="..."<NUL: if you prefer) is the most robust way of outputting a string in a batch script since it's able to handle more "poison characters" without issues. It doesn't add a line break at the end, which is why in the Before & After outputs I added &ECHO; to the end.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.