Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm having some trouble with DOS's built in batch substring utility.
For example, if I want to take a substring from a string MYVAR from index 4 to the end I would do a:
set mysubstr=%MYVAR:~4%But, what if the starting index is a variable itself? (i.e. START_INDEX is equal to 4). I have not been able to figure this out because I need to start at various places with different strings (and the index is always in a variable). Any help is appreciated.

@echo off > quit.bat
if %START_INDEX%'==' quit
goto %START_INDEX%
:1
set mysubstr=%MYVAR:~1%
quit
:2
set mysubstr=%MYVAR:~2%
quit
:3
set mysubstr=%MYVAR:~3%
quit
:4
set mysubstr=%MYVAR:~4%M2
Mechanix2Go@Golden-Triangle.com

A general way to achieve your goal is to enable the "delayed variable expansion" and so to be able to use the ! symbol other than the well known % to manage variables.
As the delayed expansion is not enabled by default you need to set it on via the /V:On switch on the tail of Cmd.exe; that implies to auto-call the batch to start the process.
The following sample should clarify what stated:
@Echo Off
If not %1.==[]. (Cmd /V:On /C Call %0 [] %1 & GoTo :EOF)
Shift
Set MyVar=ABCDEFGHKILMNOP
Set MySubStr=!MyVar:~%1!
Echo [!MySubStr!]where you have to type "V position" (e.g. V 1, V 6, V 0.....) V being the batch name. The If statement (that fits just one line) enables the delayed expansion and performs the auto-call (%0 []).
If position is another variable then the external one holds ! and the inner % i.e.
Set MySubStr=!MyVar:~%MyPos%!

IVO,
Cool beans!
Been trying to figure out delayed expansion for weeks.
M2
Mechanix2Go@Golden-Triangle.com

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |