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

Shiva
  • 20,575
  • 14
  • 82
  • 112
Phiber
  • 1,041
  • 5
  • 17
  • 40
  • answered at http://stackoverflow.com/a/5181182/492 – CAD bloke Apr 17 '16 at 01:31
  • Possible duplicate of [Removing double quotes from variables in batch file creates problems with CMD environment](http://stackoverflow.com/questions/1964192/removing-double-quotes-from-variables-in-batch-file-creates-problems-with-cmd-en) – CAD bloke Apr 17 '16 at 01:32

2 Answers2

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.

unclemeat
  • 5,029
  • 5
  • 28
  • 52
  • 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:"=%` – David Ruhmann Feb 13 '14 at 22:12
  • @Phiber if you're interested - check out the [dostips](http://www.dostips.com/DtTipsStringManipulation.php) page on string manipulation. – unclemeat Feb 13 '14 at 22:16
  • 3
    `"set command=%command:"=%"` <--- this is required for `&` poison characters – foxidrive Feb 14 '14 at 01: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](http://stackoverflow.com/a/21771437/1012053) – dbenham Feb 14 '14 at 05:24
  • @foxidrive - Removing all quotes might not be acceptable. Perhaps the command has arguments that are supposed to remain quoted. – dbenham Feb 14 '14 at 05:54
  • @dbenham It's the answer of unclemeat but the OP will see your comment if it applies to them. – foxidrive Feb 14 '14 at 08:00
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.

dbenham
  • 127,446
  • 28
  • 251
  • 390