DOS - String Variable

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


Powered by ComboStrap