How to parse a variable

upstatenewyorker used Ask the Experts™
I want to be able to parse a variable %computername%.  I want to evaluate the three leftmost characters.  If they equal "EMS" I want to goto "something".  How do I do this?  I have spent the whole morning trying to find a simple solution and so far have none.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®



You may not have found a "simple" solution
because there aren't many if you're trying to do it
using only DOS commands but I'll post a couple of methods
and if these don't do it for you then please indicate your OS and version...

Here's a batch file that uses Choice
to parse characters in a string...

This example is hard coded to get the first 3 characters
from the %computername% environment variable...

It can easily be modified to work on other variables
and/or get a different number of characters
or to accept replaceable parameters...

The errorlevel number determines the number of characters...
Change the %computername% reference to the desired string.
The final portion illustrates methods of using the goto command
as you ask and just echoes the contents of %var% to the screen...
(The first 3 parsed characters are stored in a variable named %var%)
(Also note that this isn't written to handle spaces in the variable string.)

@echo off
set var=
for %%x in (echo goto:end) do if "%computername%"=="" %%x  Variable Not Set...
if "%1"=="[?" goto loop
echo;;|choice/c?%computername%; %0;>~rin1010.bat
if "%2"=="]?" for %%x in (del goto:end) do %%x ~rin1010.bat
set var=%var%%2
if not errorlevel 3 for %%x in (shift goto:loop) do %%x
del ~rin1010.bat
if "%var%"=="EMS" goto ok
if not "%var%"=="EMS" echo    Not EMS
echo    %var%

If you want a small and simple program that will
parse a number of characters from a string
and set them to an environment variable
and do it all with one command line
then try using my Manipit utility.

I'll provide an url where you can download it...
There's no included documentation for it
but the syntax to do as you ask is:

Manipit var= left %computername%, 3

That will set an environment variable named %var%
to the first 3 characters of the %computername% variable...
Any valid variable name, number or string may be substituted...
(Note the required comma following the string that's being parsed.)

The Manipit app can be called from a batch script
having error checking for existing variables
and label branching as required...

Please post back if you want other methods
or have questions about any of these things.

Here's where you can download Manipit :



The batch file in question has to work on Windows 98, 2000, and NT operating systems.  I tried your first option there and all I got was a bunch of "out of environment space" error messages.  I do not want to tweak my computer to resolve that problem as that would mean I would have to do the same on several other '98 machines.

I would prefer going the batch file method.  I think my network admin would frown upon me using a .com file that I really knew nothing about.


IVO showed me this way.  It works wonderful.  Very simple.

@Echo Off


If ErrorLevel 1 GoTo Next2

Echo String not found
GoTo End

Echo EMS
GoTo End


Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

This can help you to verify existance of  String "EMS" in the variable and do as you desire:

@echo off
echo %computername%|find "EMS" /I>nul
if errorlevel 1 goto NO_EMS
if errorlevel 0 echo.
echo EMS exists
goto end
echo EMS not exists

Try and give feed back...never go for complicated programs.

This little batch file works for me (W2K):

set computername=EMSmycomp

if "%computername:~0,3%"=="EMS" goto something
echo not
goto end
echo ems



You mention receiving "out of environment space" errors
and not wanting to mess with maintaining a script such as that...
You can easily increase the environment size for each command instance
from within the batch file and eliminate having to check or configure each system...

But I definitely don't blame you for not wanting to waste time and worry with it
and that's why I noted there aren't many simple solutions to do as you request
when using only DOS commands -- (as opposed to third-party external utilities)
and still work under all the various operating systems as you say is necessary.
The 2K and NT Cmd.exe has extensions that can easily accomplish your task
using very few lines but the Windows 9.x DOS doesn't have such helpful tools.

The main reason that using Find wasn't initially suggested is because
you stated that you need to "evaluate the three leftmost characters."

The Find app when used in that manner will only
test whether the specified string exists
anywhere in the search string.

But if it doesn't matter where in the string the "EMS" occurs
and it works for you as needed then that's the best method.

If it helps, you don't necessarily need to pass the entire listing of the
current environment variables through Find using the Set command.
You can merely echo the %computername% variable through Find.

And if you need to perform only a single function based on your
returned Find results, you can alternatively call a procedure
from a single line when testing the Find exit codes, e.g.;

@echo off
echo %computername% | Find/i "EMS" >nul
if not errorlevel 1 echo  EMS Exists...

So please post back if you want other methods
or have further questions about branching, etc...
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- PAQ'd and points refunded
Please leave any comments here within the
next seven days.


No comment has been added lately (47 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:


Please leave any comments here within 7 days.



EE Cleanup Volunteer
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post a request in Community support (with a link to this page) to refund your points.

Finalized as proposed


Community Support Moderator
Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial