Defining and using a variable in batch file
Categories:
Mastering Variables in Batch Files: A Comprehensive Guide

Learn how to define, use, and manipulate variables in Windows batch scripts to create dynamic and powerful automation tools.
Variables are fundamental to any programming or scripting language, and batch files are no exception. They allow you to store data, such as text strings, numbers, or file paths, and reuse them throughout your script. This guide will walk you through the essentials of defining and using variables in batch files, covering both basic assignment and more advanced techniques like user input and environment variables.
Defining and Assigning Variables
In batch scripting, variables are defined and assigned values using the SET
command. The basic syntax is straightforward: SET VariableName=Value
. Variable names are not case-sensitive in batch files, but it's good practice to maintain consistent casing for readability. To access the value of a variable, you enclose its name in percent signs, like %VariableName%
.
@echo off
SET MyString=Hello, Batch!
SET MyNumber=123
SET FilePath=C:\Users\Public\Documents
ECHO %MyString%
ECHO The number is: %MyNumber%
ECHO Files are in: %FilePath%
Basic variable definition and usage in a batch file.
=
) when using SET VariableName=Value
. SET VariableName = Value
will include the spaces in the variable name and value, leading to unexpected behavior.User Input and Dynamic Variables
Batch files can interact with users to gather input, making scripts more flexible. The SET /P
command is used to prompt the user for input and store it directly into a variable. This is invaluable for creating interactive scripts that adapt based on user choices.
@echo off
SET /P UserName="Please enter your name: "
ECHO Hello, %UserName%!
SET /P Age="How old are you? "
ECHO You are %Age% years old.
Using SET /P
to get user input.
flowchart TD A[Start Batch Script] --> B{"Prompt for User Name?"} B -- Yes --> C["SET /P UserName=\"Enter Name: \""] C --> D["ECHO Hello, %UserName%!"] B -- No --> D D --> E{"Prompt for Age?"} E -- Yes --> F["SET /P Age=\"How old are you? \""] F --> G["ECHO You are %Age% years old."] E -- No --> G G --> H[End Script]
Flowchart illustrating user input in a batch script.
Environment Variables
Windows provides a set of predefined environment variables that contain useful system information, such as the current user, system directory, or temporary file paths. These variables can be accessed just like user-defined variables, using the %VariableName%
syntax. Common environment variables include %USERNAME%
, %TEMP%
, %SYSTEMROOT%
, and %PATH%
. You can also define your own environment variables, which persist beyond the current script execution (though typically only for the current command prompt session unless set globally).
@echo off
ECHO Current User: %USERNAME%
ECHO Temporary Directory: %TEMP%
ECHO System Root: %SYSTEMROOT%
ECHO Path Variable: %PATH%
REM Define a temporary environment variable for the current session
SET MY_APP_PATH=C:\Program Files\MyApp
ECHO My App Path: %MY_APP_PATH%
Accessing and defining environment variables.
PATH
) directly from a batch file can have unintended consequences if not done carefully. Always back up your system or test changes in a controlled environment.