DOS - String Variable

Card Puncher Data Processing

About

Text - String data type in DOS

Manipulation

Replace

When you want to use the time, you will get colon that you can't use in a file name. To overcome this problem, you can replace the colon with an underscore.

set theTime=%time%
echo theTime
15:04:31.11

:: Replace : with _
echo %theTime::=_%
15_04_31.11

Slice

The slice operation syntax is:

%myVariable:~indexFirst,numberOfLetters %

where:

  • the ~ tilde indicates the slice function
  • indexFirst indicates the position of the first letters (It can be negative)
  • numberOfLetters indicates the number of letters (It can be negative)

Example:

set theTime=%time%
echo %theTime%
15:12:16.36

  • Extracting the substring from the third position with 2 letters length.
echo %theTime:~3,2%
12
  • Removing the last three characters (From the beginning to the end minus three characters)
echo %theTime:~,-3%
15:12:16

  • Removing the first three characters:
echo %theTime:~3%
12:16.36

  • Extracting the last three character
echo %theTime:~-3%
.36

Comparison

See text and number comparison





Discover More
Card Puncher Data Processing
DOS ErrorLevel - How to manage errors with the exit code ?

This article shows you how to manage error handling with the exit code of DOS. In dos: When the errorlevel is: * = 0, then No Error occurred * > 0, then an error occurred You can test if with...



Share this page:
Follow us:
Task Runner