colon tilde in windows batch scripting

Tilde is an interesting symbol in windows batch script. You may have seen %~0. The tilde before the digit (which means the command name) does something  special to the parameter. Colon is also a useful symbol in windows bat file. Colon is used to replace a substring of a variable with another. So %var:substring1=substring2% will replace all occurrences of substring1 of %var% with  substring2. What about the colon-tilde combination?  :~ is used to get a substring of a variable. So %var:~0,1% will get the first character of %var%. %var:~0,2% will get the first two characters of %var%. %var:~1,2% will get the substring starting from index 1 and of the length 2. %var:~0,-1% get the sub-string starting from index 0 to(excluding) the last character so %var:~0,-1% actually remove the last character of %var%. %var:~10% get the sub-string from index 10 to the end of %var%. For more details, please refer to this post.

To view this content, you must be a member of Thomas's Patreon at $5 or more
Already a qualifying Patreon member? Refresh to access this content.
Posted in

Leave a Reply