Batch SET Command Guide: Variables & Examples

In a batch BAT script, the set command is used to set environment variables or assign values to variables. Here are some examples of how the set command can be used:

  1. Set up environment variables:
set MYVAR=value
  1. Display all environment variables.
set
  1. Display the value of a specific environment variable:
echo %MYVAR%
  1. Save the output of a command to a variable.
for /f "tokens=*" %%a in ('dir /b') do set FILES=%%a
  1. Utilize delayed expansion variables.
setlocal enabledelayedexpansion
set MYVAR=123
echo !MYVAR!

It’s important to note that variable names are not case-sensitive in batch scripts. Additionally, variables set using the set command are only valid within the current batch script. To use them in other scripts or command lines, you can use the setx command to set the environment variable as a global variable.

bannerAds