|
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% In Win 2K and XP this can include negative numbers: %variable:~num_chars_to_skip, -num_chars_to_skip% %variable:~-num_chars_to_skip,num_chars_to_keep% A negative number will count backwards from the end of the string.
Examples
Those that require Win 2K/XP are indicated with ##
The variable v_test is used for all the following examples:
SET v_test=123456789abcdef0 ::Extract only the first 5 characters SET v_result=%v_test:~0,5% ECHO %v_result% =12345 ::Skip 7 characters and then extract the next 5 SET v_result=%v_test:~7,5% ECHO %v_result% =89abc ::Skip 7 characters and then extract everything else SET v_result=%v_test:~7% ECHO %v_result% =89abcdef0 ::Extract only the last 7 characters ## SET v_result=%v_test:~-7% ECHO %v_result% =abcdef0 ::Extract everything BUT the last 7 characters ## SET v_result=%v_test:~0,-7% ECHO %v_result% =123456789 ::Extract between 7 from the front and 5 from the end ## SET v_result=%v_test:~7,-5% ECHO %v_result% =89ab ::Go back 7 from the end then extract 5 towards the end ## SET v_result=%v_test:~-7,5% ECHO %v_result% =abcde ::Extract between 7 from the end and 5 from the end ## SET v_result=%v_test:~-7,-5% ECHO %v_result% =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 - Edit string variables,
PATH and parameter
variables