NT Syntax : Variable parsing

Parsing allows the retrieval of any part of a string variable.

syntax
      %variable:~num_chars_to_skip%
      %variable:~num_chars_to_skip,num_chars_to_keep%
      %variable:~num_chars_to_skip, -num_chars_to_skip%
      %variable:~-num_chars_to_skip,num_chars_to_keep%

## In Win 2K and XP the number of characters may be a negative number
to count backwards from the end of the string.

This is best explained with a few examples:

The variable v_test is used for all the following examples:

set v_test=123456789abcdef0

Extract only the first 5 characters

   %v_test:~0,5%
   12345

Skip 7 characters and then extract the next 5

   %v_test:~7,5%
   89abc

Skip 7 characters and then extract everything else

   %v_test:~7%
   89abcdef0

Extract only the last 7 characters ##

   %v_test:~-7%
   abcdef0

Extract everything BUT the last 7 characters ##

   %v_test:~0,-7%
   123456789

Extract between 7 from the front and 5 from the end ##

   %v_test:~7,-5%
   89ab

Go back 7 from the end then extract 5 towards the end ##

   %v_test:~-7,5%
   abcde

Extract between 7 from the end and 5 from the end ##

   %v_test:~-7,-5%
   ab

Advanced Usage of :~

You can use the :~ syntax and provide each of the parameters from other variables, for example if you have

%v_donor%=5522950
%v_digit%=4

To extract digit # 4 from v_donor you might try

SET v_char=%v_donor:~%v_digit%,1%

Unfortunately this will not work because the :~ syntax expects a value not a variable. To get around this use the CALL command like this:

 SET start_char=2
 SET length=1
 SET v_donor=884777
 CALL SET substring=%%v_donor:~%start_char%,%length%%%
 ECHO (%substring%) 

Related Commands:

SEARCH STRING - Editing string variables is discussed in more details on this page - (also covers the PATH variable and parameter variables)



Simon Sheppard
SS64.com