How to Remove Double Quotes From Variables in a Batch File

Siddharth Bishnoi Feb 02, 2024
  1. Remove Double Quotes Using the Tilde Character in a Batch File
  2. Remove All Surrounding Double Quotes From the Variables in a Batch File
  3. Remove Double Quotes Using the FOR Loop in a Batch File
How to Remove Double Quotes From Variables in a Batch File

In Batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we don’t want these quotes to be seen in the output. These quotes can be eliminated from variables in Batch files.

There are many ways to remove double quotes from variables. Some methods create problems with the CMD environment, which may lead to errors in your Batch file.

We will be discussing all the methods and possibilities. This tutorial will discuss removing double quotes from variables in a Batch file.

Remove Double Quotes Using the Tilde Character in a Batch File

Double quotes can be removed from variables in a Batch file using the tilde (~) character. The tilde (~) character is used in many ways in Batch files.

Apart from being an argument quote removal, it is also used to extract a substring from a string variable in a Batch file. To remove quotes from variables, use %~ before the command line argument from which quotes need to be removed.

It removes any surrounding quotes from the parameter.

set A=%~1
set parameters=%~2 %~3
%A% %parameters%

Here, %1 specifies the first command line parameter passed on to the Batch file, %2 specifies the second command line parameter, and so on. It only handles parameters till %9.

But, this method works only for the command parameter passed into Batch files, not for the variables. Users often use this for variables, leading to errors during the Batch file execution.

Remove All Surrounding Double Quotes From the Variables in a Batch File

Another way of removing double quotes from variables is by using %var:"=% in Batch files. This can be used to remove double quotes from any string or variables.

You can either remove quotes from any of the ends, i.e., outer quotes, or remove all quotes at once. The basic syntax will be:

set variable=%variable:"=%

The following example shows how to remove all quotes from a variable in a Batch file:

set var1="Hello World"
set var1
set var1=%var1:"=%
set var1
PAUSE

Remove All Quotes From a Variable

But the above code will not work if the variable has a & character.

Apart from removing double quotes from variables, you can also replace them with single quotes or any other character. You can also replace words in the variables using the above code.

Replace Double Quotes With Single Quotes in a Batch File

To replace double quotes with single quotes, add the single quote after the equal sign, as shown in the example below.

set var1="Hello World"
set var1
set var1=%var1:"='%
set var1
PAUSE

Replace Double Quotes With Single Quotes

Replace Words From Variables in a Batch File

To replace a word from a variable in a Batch file with any other word, modify the above code as shown in the following example:

set var1="Hello World"
set var1
set var1=%var1:World=Hannah%
set var1
PAUSE

Replace a Word in a Variable

Remove Double Quotes Using the FOR Loop in a Batch File

The double quotes can also be removed using the for loop from a variable in Batch files. Add the following line in your Batch file, as shown in the example below, to remove any double quotes from variables.

SET var1="Hello World"
ECHO %var1%
FOR /F "delims=" %%I IN (%var1%) DO SET new_var1=%%I
ECHO %new_var1%
PAUSE

Remove Double Quotes Using FOR Loop

You can also create a Batch file containing the single line code for the FOR loop to remove double quotes and save it as a .cmd file. Further, using the CALL command, you can use it in another Batch file directly to remove double quotes from variables.

Note that this will not work for mismatched quotes. You can refer to the SS64 forum by clicking here to know more.

So, we discussed how to remove double quotes from variables in a Batch file using different methods. Also, the limitations are mentioned at the end of each method.

Related Article - Batch Variable