How to manage variable in dos? Environment variable

Card Puncher Data Processing


How to manage variables in dos.

Naming convention

A variable name:

  • must only be made up of:
    • alphabetic characters,
    • numeric characters
    • and underscores,
  • cannot begin with a numeric character.
  • cannot be a system variable (such as if, for,…)



One variable

echo %myVariable%

of when delayed variables expansion is used.

echo !myVariable!

All variables (with optional pattern filtering)

Use the set command with or without a search term to filter the output:

set  <search term>


set W


A session variable

In line

A session variable has the scope of the console. If the console is closed the variable will be deleted.

set myVariable=myValue
from a file

with Dos - For Statement

for /f "delims=" %%x in (myFile.txt) do set myVariable=%%x


An environment variable

If you want to preserve the value of a variable through several console, you have to set an environment variable with the setx command.

(Unset|Delete) a variable

  • Create a variable
set test=1
>echo %test%
  • Delete it
>set test=
>set test
Environment variable test not defined


By using the set command, the variable have a session scope. If you want a global scope, you must use the setx command

And in an block, the set command will not set the expected value if you don't use the Delayed environment variable expansion.

How to

Test if a variable is set

With the if defined variable statement.


if not defined v (echo v is not defined) else echo %v%
v is not defined

set v=Call Me Nico
if not defined v (echo v is not defined) else echo %v%
Call Me Nico

Get a sub-string of a variable

set test=123456
echo %test:~1,5%


  • the first number defines the first position (default to 0)
  • the second number defines the number of character to return (default to the length of the variable)

Unset multiple variables at once

By using the command parsing possibility of the for command. I have the following variable that I want to (unset|delete)

set HI

You can do it with the following snippet:

FOR /F "usebackq delims==" %i IN (`set HI`) DO set %i=

The command must be between back quoted characters


set 1=Hello & echo %1% Nico 

Discover More
Card Puncher Data Processing
DOS - Configuration

path environment variable to find the scripts
Card Puncher Data Processing
DOS - Parsing (File, Command, Variable) - FOR F option

parsing in DOS is done via the F option of the FOR command where: the option /F means (file|text) processing % and %% indicates that we are in presence of a variable % is used in on-line mode...
Card Puncher Data Processing
DOS - SET Command

Displays, sets, or removes Windows environment variables. Set a Windows 8.3 Pathname “” You need to use the Windows 8.3 Pathname notation. See
Card Puncher Data Processing
DOS - Special Characters

This article tries to list all special characters and their meanings. Character Description Articles | pipeline > stdout < stdin 2> stderr >> append & duplicate handle or control...
Card Puncher Data Processing
Dos (Win32 Shell Scripting) and Utilities

Dos is a shell language of the Windows cmd interpreter. Ported on Windows
Card Puncher Data Processing
Dos - Command line (Argument|Parameter)

Command line arguments for DOS are passed to: the batch script through the cmd or call command: the function (subroutine) through the call command: variablesIf Defined Variable statement “”...
Card Puncher Data Processing
Dos - Date and Time

To get the current date and time, use the two following variables: %DATE% - expands to current date using same format as DATE command. %TIME% - expands to current time using same format as TIME command....
Card Puncher Data Processing
Dos - Dir

Dir displays a list of files and subdirectories in a directory. where the options without extra attributes are: Options Description [drive:][path][filename] Specifies drive, directory, and/or...
Card Puncher Data Processing
Dos - Dynamic Variable

Dynamic Variable are managed by the interpreter in order to give access to environment variable but theyare not variable in the way that you can't set them in order to change their value. %CD%: the...
Card Puncher Data Processing
Dos - Echo

Displays messages, or turns command-echoing on or off. escape Just add the special characters bracket or period just after the echo command You can echo in color with color terminal escape sequence...

Share this page:
Follow us:
Task Runner