If you are using Microsoft Windows, such as Windows XP, you can
extract a substring from a variable string using character positional
%myvar:~char_skip_num%. I.e., where you have
some variable, e.g.
%myvar%, you can place the
:~ operator prior to the ending
% and then specify
a the number of characters to skip followed by the ending
E.g. from a command line if you checked the value of a user profile variable,
%userprofile%, for a user named James, you might see the
C:\>echo %userprofile% C:\Documents and Settings\James
If I wanted to extract just the user's account name, i.e.
I could use the following:
C:\>echo %userprofile:~26% James
The "J" in
James is the 27th character,
so I used
26 to have the first 26 characters skipped.
You can also specify that you only want to extract a certain number of characters by putting a comma after the number of characters to be skipped followed by the number of characters to extract. E.g, if I only wanted to extract the first 3 characters starting at position 27, I could use the following:
C:\>echo %userprofile:~26,3% Jam
If you don't want to skip any characters, but specify only a certain number
of characters to extract, you can use the syntax
where you specify that zero characters are to be skipped and
represents the number of characters to extract from the string.
C:\>echo %userprofile:~0,3% C:\
You can also specify that you want to start the extraction from the end of
the line rather than the beginning by using
x is some number, for the starting position. E.g. you could
use the following to extract the last 3 characters from the line:
C:\>echo %userprofile:~-3% mes
You can specify the number of characters to extract, just as noted before, with this method as well. E.g. to extract the substring that starts at 3 characters from the end of the string, but only includes 2 characters from that point, the following could be used:
C:\>echo %userprofile:~-3,2% me
If you are using a batch file, e.g.
you could display just the desired part of the string as follows:
When the batch file is executed, it wold display just "me"
For further examples, see Variables: extract part of a variable (substring)
part of a variable (substring)
Substrings in Windows Batch Files