Computing.Net > Forums > Programming > DOS Batch substring problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

DOS Batch substring problem

Reply to Message Icon

Name: VernonH
Date: September 15, 2004 at 12:17:18 Pacific
OS: XP SP1 / NT4
CPU/Ram: Pentium M 1.5 GHz
Comment:

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.




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 15, 2004 at 15:01:52 Pacific
Reply:

@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



0

Response Number 2
Name: IVO
Date: September 16, 2004 at 00:59:04 Pacific
Reply:

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%!



0

Response Number 3
Name: Mechanix2Go
Date: September 16, 2004 at 01:09:53 Pacific
Reply:

IVO,

Cool beans!

Been trying to figure out delayed expansion for weeks.

M2

Mechanix2Go@Golden-Triangle.com



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: DOS Batch substring problem

MS Dos batch Script Help www.computing.net/answers/programming/ms-dos-batch-script-help/14592.html

DOS - For Loop Problem www.computing.net/answers/programming/dos-for-loop-problem/17488.html

For loop in DOS batch program www.computing.net/answers/programming/for-loop-in-dos-batch-program/18227.html