2

What do you call this in batch file scripting ? %VARIABLE:~2%

So I'm trying to figure what this is specifically called when you set a variable, then select a position from that variable?

Example

SET TEST=FOO BAR 123
SET VAR=%TEST:~4,3%

And if I echo %VAR% I know it will return "BAR", but I have no idea what this is called, I just had to figure it out on my own by changing the values and checking what it returned.

Thank you.

Hackoo
  • 18,337
  • 3
  • 40
  • 70
arealhobo
  • 447
  • 1
  • 6
  • 17
  • 2
    Will this be helpful for you? http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file – Tanaike Jan 28 '17 at 02:22
  • I believe that's it! Never knew what it was called but that seems about right. Doing a search for "Variable Substring" results in what I was looking for. Thank you! – arealhobo Jan 28 '17 at 02:47
  • Perhaps this link might help: http://ss64.com/nt/syntax-substring.html – aschipfl Jan 28 '17 at 03:44

1 Answers1

1
Microsoft Windows [Version 10.0.10240] (c) 2015 Microsoft Corporation. All rights reserved.

C:\Windows\system32>set /? 

Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

  variable  

Specifies the environment-variable name.   string    Specifies a series of characters to assign to the variable.

Type SET without parameters to display the current environment variables.

If Command Extensions are enabled SET changes as follows:

...
 
Environment variable substitution has been enhanced as follows:

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2".  "str2" can
be the empty string to effectively delete all occurrences of "str1"
from the expanded output.  "str1" can begin with an asterisk, in which
case it will match everything from the beginning of the expanded
output to the first occurrence of the remaining portion of str1.

May also specify substrings for an expansion.

    %PATH:~10,5%

would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the
 expanded result.  If the length is not specified, then it defaults to
the remainder of the variable value.  If either number (offset or
length) is negative, then the number used is the length of the
environment variable value added to the offset or length specified.

    %PATH:~-10%

would extract the last 10 characters of the PATH variable.

    %PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

...

C:\Windows\system32>
Community
  • 1
  • 1
Freddie
  • 269
  • 1
  • 4