It is possible to retrieve specific characters from a string variable.
Syntax %variable:~start_index% %variable:~start_index,length% This can include negative numbers: %variable:~start_index, -length% %variable:~-start_index,length% %variable:~-start_index,-length%
start_index Defines the starting point for the string returned, for positive numbers this is the number of characters skipped.
A negative number will count backwards from the end of the string.
length Defines the number of characters to return as a string. For positive numbers this is the number of characters to return after the start_index, for negative numbers this is a number of characters to return counting back from the end of the string.
This syntax adds a little extra flexibility compared to a standard Mid() function, which is best illustrated by the examples below.
The variable _test is used for all the following examples:
SET "_test=123456789abcdef0" ::Extract only the first 5 characters SET "_result=%_test:~0,5%" ECHO %_result% =12345 ::Skip 7 characters and then extract the next 5 SET "_result=%_test:~7,5%" ECHO %_result% =89abc ::Skip 7 characters and then extract everything else SET "_result=%_test:~7%" ECHO %_result% =89abcdef0 ::Extract only the last 7 characters SET "_result=%_test:~-7%" ECHO %_result% =abcdef0 ::Extract everything BUT the last 7 characters SET "_result=%_test:~0,-7%" ECHO %_result% =123456789 ::Extract between 7 from the front and 5 from the end SET "_result=%_test:~7,-5%" ECHO %_result% =89ab ::Go back 7 from the end then extract 5 towards the end SET "_result=%_test:~-7,5%" ECHO %_result% =abcde ::Extract between 7 from the end and 5 from the end SET "_result=%_test:~-7,-5%" ECHO %_result% =ab
This variable substring syntax only works for CMD environment variables, like %MYVAR%, it will not work with FOR parameter variables, like %%G, however a simple workaround is to set a variable first: Set MYVAR=%%G and then find the substring of the new variable.
If you need to access the current value of a variable within a FOR loop, then you may need to use delayed expansion. By default variable expansion will happen just once at the start of each loop.
The start_index may be omitted, defaulting to zero, though many prefer to include it to help document what the command is doing.
Advanced Usage of :~
You can use the :~ syntax and provide each of the parameters from other variables, for example if you have:
Set "_donor=88477" Set "_digit=2"
To extract digit # 2 from %_donor% you might try:
SET "_substring=%_donor:~%_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:
@Echo Off SET _donor=884777 SET _startchar=2 SET _length=1 CALL SET "_substring=%%_donor:~%_startchar%,%_length%%%" ECHO (%_substring%)
:: Credits:
:: Clay Calvert - alt.msdos.batch.nt
:: Ritchie Lawrence - alt.msdos.batch.nt
Don’t build your castle in other people’s kingdoms ~ Chris Zukowski (zukalous)
How-to: VarSearch - Search & replace part of a variable.
How-to: strlen.cmd - Get string length.
PowerShell equivalent: Substring() - Return part of a longer string.