JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Remove double quotes from variable in batch file

Author:JIYIK Last Updated:2025/03/22 Views:

In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files.

There are many ways to remove double quotes from a variable. Some methods will cause problems with the CMD environment, which may cause errors in your batch file.

We will discuss all the methods and possibilities. This tutorial will discuss removing double quotes from variables in batch files.


Remove double quotes using tilde character in batch file

You can use the tilde ( ~) character to remove double quotes from a variable in a batch file. The tilde ( ~) character is used in a variety of ways in batch files.

In addition to removing argument quotes, it is also used to extract substrings from string variables in batch files. To remove quotes from a variable, use %~ before the command line argument from which you need to remove quotes.

It removes any surrounding quotes from the arguments.

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

Here, %1you specify the first command line argument passed to the batch file, %2specify the second command line argument, etc. It only processes the arguments before %9.

However, this method only works for command arguments passed into a batch file, not for variables. Users often use this for variables, resulting in errors during batch file execution.


Remove all surrounding double quotes from a variable in a batch file

Another way to remove double quotes from a variable is by using %var:"=% in a batch file. This can be used to remove double quotes from any string or variable.

You can remove the quotes from either end, the outer quotes, or remove all of them at once. The basic syntax would 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 if the variable has a &character, the above code will not work.

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

Replace double quotes with single quotes in batch file

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

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

Replace double quotes with single quotes

Replace word from variable in batch file

To replace a word in 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


Using FOR loop in batch file to remove double quotes

You can also remove double quotes using a for loop with variables in a batch file. Add the following line in your batch file as shown in the example below to remove any double quotes from the variable.

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 with a single line of code with a FOR loop to remove the double quotes and save it as a .cmd file. Also, using the CALL command, you can directly use it in another batch file to remove the double quotes from a variable.

Hence, we have discussed how to remove double quotes from a variable in batch file using different methods. Also, limitations are mentioned at the end of each method.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Batch file to loop through files in subdirectories

Publish Date:2025/03/22 Views:96 Category:OPERATING SYSTEM

This article explains how we can write a batch script to loop through files in subdirectories. We will take an example to explain the concept. Batch file to loop through files in subdirectories Assume we have the directory structure shown b

Batch file to remove X characters from a file name

Publish Date:2025/03/22 Views:153 Category:OPERATING SYSTEM

This article explains how we can remove specific characters from the filename of a file using a batch script. We will cover several methods below to rename files on Windows. File Explorer Renaming on Windows File Explorer offers the least f

Stop a running process from a batch file

Publish Date:2025/03/21 Views:201 Category:OPERATING SYSTEM

This article explains how to stop a running process from a batch file in Windows. We use Batch's taskkill command to terminate the running process. 请注意 , the command will be executed only if the specified process is open. Batch file t

Get the current batch file directory

Publish Date:2025/03/21 Views:125 Category:OPERATING SYSTEM

This article demonstrates how to determine the location of a batch file. Batch scripts are great for automation. Sometimes you may need to get the location of a batch file. This article will help you determine the working directory and batc

Running batch files remotely

Publish Date:2025/03/21 Views:176 Category:OPERATING SYSTEM

Sometimes we need to use a computer remotely from a different location. We can use some third-party software such as TeamViewer to do this. But we can execute batch files from a remote directory without the need for third-party software. In

Run the batch file as administrator

Publish Date:2025/03/20 Views:142 Category:OPERATING SYSTEM

Batch files consist of commands that are executed by the Command Prompt. Some of these commands cannot be run without administrator privileges; therefore, it is important to run batch files as an administrator. You can manually run a batch

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial