Forum Discussion

Sincerer's avatar
Sincerer
Iron Contributor
Jun 17, 2025

Windows CMD shell and variable expansion with substring (~)

Hi. I am trying to learn about Windows batch files and I am looking for some more complete documentation. I have found coding examples for pulling substrings out of longer strings and saving them in environment variables. See below example which demonstrates the substring syntax to extract the year, day, and month from the date.

set my_year=%date:~0,4%
set my_day=%date:~8,2%
set my_month=%date:~5,2%

While this is useful I would like to read and learn more about this syntax but I can't seem to find anything that more fully describes (i.e. what else can is possible with this syntax).
Can anyone point me towards a book or Microsoft documentation that goes into more detail about this substring syntax?

1 Reply

  • CameronFord's avatar
    CameronFord
    Iron Contributor

    Experiment with different start and length values to see how the substring extraction behaves. Also, try combining pattern replacements and substring extraction for complex parsing tasks.

Resources