JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Difference between % and %% in batch files

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

Batch programmers often confuse single percent signs (%) and double percent signs (%%) when used in batch files. The FOR command uses %f when executed on the command line, but in a batch file, it uses %%f instead of a single percent sign.

The symbols %, %%, , %variable%and !variable!have some basic differences and are used differently on the command line and in batch files.

This article explains the difference between %and in batch files %%. It also discusses the use of %and in batch files and cmd %%, as well as environment variables and loop variables.


Using % and %% in CMD

In a command shell, % variables specify single-letter replaceable parameters. Ms-DOS uses %1,%2,...,%9 as replaceable command line parameters, that is, %1 will be replaced by the first parameter passed to the batch file.

The following example shows that a single percent sign with a variable (%a) acts as a null character:

set a="Hello World"
echo %a

Using % in CMD

When percent signs are used around a variable, %VARIABLE%, it acts as an environment variable and displays the set value when used with the echo command. %VARIABLE% can be set using the set command and accessed using the echo command.

An example is shown below.

set a="Hello World"
echo %a%

Output - Use % in CMD


Using % and %% in batch files

In a batch file, the command line shell reads all commands from left to right. If it reads a percent sign, it moves on to the next character.

It will %%read as a single %ie %%replace with a single percent and read the third character. If it is in a FOR loop it will evaluate it like in a FOR loop.

The following example explains the use of %% in a FOR loop.

FOR /L %%3 in (1,1,3) Do Echo %%3
PAUSE

Using %% in a for loop

Output:

Output - Using %% in a for loop

If not a FOR loop, it is read as an ordinary character and behaves the same as the %variable command when executed directly in a command-line shell.

set a="Hello World"
echo %%a
PAUSE

Use %% as a normal character

Output:

Output - Use %% as normal characters

If the character after the % sign is a number, it is read as a command line argument. If the character after the % sign is neither a % sign nor a number, it is read as a variable until the next % sign; then, its value is displayed.

For example,

set a="Hello World"
echo %a%
PAUSE

Use the % symbol to represent variables

Output:

Output - Use % sign for variables

To use pure % signs (such as 50%) in a batch file, you need to use %% instead of a single % sign.

set a="The battery is charged 50%%"
echo %a%
PAUSE

Read the % symbol in the string

Output:

Output - Read the % symbol in the string

%and %%are mainly used in the FOR command. When the FOR command is used outside a batch file, a single %symbol works well.

However, when used in a batch file, it should be replaced with double %% to avoid any errors. Also, it is recommended to avoid using numbers with % sign as FOR command parameters.


Using delayed expansion in batch files

Sometimes, using %VARIABLE% in a batch file does not print the result. This is because when percent expansion is done, the variable is expanded before execution (i.e. at parsing time).

When used in a loop, the variable is expanded only once.

To solve this problem, you can use delayed expansion, which !variable!replaces percent signs ( ) with exclamation points ( %variable%). When delayed expansion is enabled, variables are expanded at execution time.

In a loop, it is expanded at each iteration. Therefore, it is recommended to use % instead of ! to expand variables, mainly when using loops.

To enable delayed expansion, we can use the SETLOCAL EnableDelayedExpansion command and replace % with !, as shown in the following example.

@echo off
setlocal EnableDelayedExpansion
:: count to 5, storing results in a variable
set n=0
FOR /l %%G in (1,1,5) Do (echo [!n!] & set /a n+=1)
echo Total = %n%

Using Delayed Expansion

Output:

Output - Use of delayed extension

So, we discussed the difference between the %and %%symbols when used in batch files and cmd. We covered almost everything including the use of delayed expansion.

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

The difference between Git pull and Git clone

Publish Date:2025/03/26 Views:193 Category:Git

This tutorial will discuss the difference between the git clone and commands. git pull git pull Using commands in Git We use git pull the command to get updates from the remote to the local. This command will update the files in the local r

Reading file into variable in batch script

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

Sometimes, we need to put the entire contents of a file into a variable for various purposes, such as finding specific data from a file, replacing a specific part of a file, etc. In Batch, it is very easy to put the entire file contents in

Declaring variables in batch script

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

This article will demonstrate how to declare and define variables in a batch script. Declaring variables in batch script In Batch, you do not need to use any additional keywords to declare integer, float, double, or string type variables. T

Replace text in a file in batch script

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

In this article, we will look at some ways to replace text in a file. We will look at two different ways. Our first approach consists only of a batch script to perform the task, while the second approach provides a solution through Windows

Running batch scripts in C#

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

In this article, we will see how to write a C# program that can run a batch file from a directory. Running batch scripts in C# In C#, when we want to execute a batch file, it acts as a process. You can follow the sample code below to run a

Extract or unzip files in batch script

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

In this article, we will see how to create a batch script to extract files from a zip file. Create a batch script to unzip the files We can extract or decompress files using batch scripts which need to contain the destination directory wher

Transferring files to Raspberry Pi using batch script

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

This article will show a batch command that transfers a file from Windows to the Raspberry Pi environment and executes the file. PuTTY is a third-party free tool that implements Telnet and SSH for Windows and Unix platforms for free. It als

Renaming part of a file name in a batch script

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

Sometimes we need to rename a series of files in a specific order. Most project files contain this sequence, and we can easily do this in a batch script using a simple one-line command. This short article will show us how to change a specif

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial