16

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

2 Answers 2

40

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.

6
  • 4
    +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:"=% Feb 13, 2014 at 22:12
  • @Phiber if you're interested - check out the dostips page on string manipulation.
    – unclemeat
    Feb 13, 2014 at 22:16
  • 3
    "set command=%command:"=%" <--- this is required for & poison characters
    – foxidrive
    Feb 14, 2014 at 1:29
  • 1
    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
    – dbenham
    Feb 14, 2014 at 5:24
  • @foxidrive - Removing all quotes might not be acceptable. Perhaps the command has arguments that are supposed to remain quoted.
    – dbenham
    Feb 14, 2014 at 5:54
10

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
  • 1
    I was wondering why this wasn't working. Needed to "setlocal enabledelayedexpansion"
    – leeman24
    Jul 28, 2016 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.