Creating Variables
Do you find yourself repeating the same string multiple times when working with batch files?
For example, when copying a specific file and then moving it to a copied folder, you end up writing the same path multiple times like this:
copy C:\Users\user\Documents\sample.txt C:\Users\user\Documents\copy\sample.txt
move C:\Users\user\Documents\sample.txt C:\Users\user\Documents\copied\sample.txt
When copying and moving, C:\Users\user\Documents\sample.txt
is written twice.
Although this is not a problem, writing the same string multiple times can cause typing errors and make corrections difficult.
To solve this problem, variables can be defined in batch files.
By defining variables, there is no need to write the same string multiple times, reducing the risk of typing errors.
Rewriting the previous example using variables would look like this:
set source=C:\Users\user\Documents\sample.txt
copy %source% C:\Users\user\Documents\copy\sample.txt
move %source% C:\Users\user\Documents\copied\sample.txt
The variable source
is assigned to the string C:\Users\user\Documents\sample.txt
, which was previously written twice.
By doing this, simply writing %source%
will have the same meaning as C:\Users\user\Documents\sample.txt
.
This page introduces how to define variables in batch files and provides specific examples of using variables.
How to Define Variables
To define variables, use the set
command.
set variable_name=value
After the set
command, specify the variable name and value.
Do not put spaces before or after the equals sign.
Options for the set Command
In addition to defining variables in advance as mentioned above, the set
command can also accept user input when executed as a batch file, depending on the specified options.
/P Option
By specifying the /P
option, you can accept input from the user.
set /P variable_name=display_message
The above command does not mean that the string “display_message” is assigned to a variable named “variable_name”.
By specifying the /P
option, the set
command accepts input from the user.
The “display_message” part is the message displayed on the screen when accepting a value to store in a variable.
When the above batch file is actually executed, it will be displayed as follows:
The batch file waits until it receives input from the user, and no processing is performed after the set
command.
When the user enters a value and presses Enter, the entered value is assigned to the variable, and processing after the set
command is executed.
Specific Examples Using Variables
Concatenating Strings
Variables and Strings
To concatenate variables and specific strings, no operator is required, and write %variable_name%string
.
set source=C:\Users\user\Documents\sample
echo %source%.txt
Variables and Variables
For concatenating variables and variables, write %variable_name1%%variable_name2%
.
set source=C:\Users\user\Documents\sample
set extension=.txt
echo %source%%extension%
Calculating Mathematical Expressions
You can perform mathematical calculations using the set /A
command to store the results of the calculation in a variable.
@echo off
setlocal
set x=0
for /L %%i in (1, 1, 10) do (
set /A x=x+%%i
)
echo %x%
endlocal
When you run the above batch file, it will be displayed as follows:
Using Environment Variables
In batch files, you can use predefined environment variables in addition to variables that you define yourself.
Environment variables are variables provided by Windows that store information about Windows.
List of Environment Variables
Environment Variable | Description |
---|---|
%CD% | Path of the current directory |
%DATE% | Current date |
%TIME% | Current time |
%RANDOM% | Random number between 0 and 32767 |
%ERRORLEVEL% | Exit code of the last executed command |
%USERNAME% | Current username |
%COMPUTERNAME% | Computer name |
%OS% | Type of operating system |
%PROCESSOR_ARCHITECTURE% | Type of processor |
%NUMBER_OF_PROCESSORS% | Number of processors |
%SYSTEMROOT% | Path of the system folder |
%TEMP% | Path of the temporary folder |
%PATHEXT% | List of executable extensions |
%PATH% | Path of the executable files |
%HOMEDRIVE% | Drive of the home folder (usually “C:”) |
%HOMEPATH% | Path of the home folder |
%USERPROFILE% | Path of the user folder |
%APPDATA% | Path of the application data folder |
%PROGRAMFILES% | Path of the program files folder |
%COMMONPROGRAMFILES% | Path of the common program files folder |
%COMMONPROGRAMFILES(x86)% | Path of the common 32-bit program files folder |
%PUBLIC% | Path of the public folder |
%ALLUSERSPROFILE% | Path of the common user folder |
%userprofile%
and others are commonly used, so it is useful to remember them.
Examples of using environment variables
Creating a file in the user’s home folder
@echo off
setlocal
cd %userprofile%
dir
endlocal