Branched Operate Based on Variable First Letter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Branched Operate Based on Variable First Letter

#1 Post by Samir » 17 Mar 2018 00:06

I have a batch in which a variable with have a directory name. That directory name will start with a letter and vary in length, but usually within the old dos 8.3 limits.

For example, variable name could be one of the following:

Code: Select all

ALMOND
ARYA
CHEVA
SATYA
What I would like to do is if the first letter of this variable is less than the letter 'P' it branches to do one thing, and if it is after 'P', it does another branch.

So with the above example variables, SATYA would take branch 2 versus the rest going on branch 1. The variable would only be presented one at a time (passed via a CALL :FUNCTION "VARIABLE"), and not like a list as shown above.

I have a few ideas on how to just raw-hack this to work, but I know there's some elegant way to do this that I couldn't find in a search. :(

Any assistance appreciated. 8)

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Branched Operate Based on Variable First Letter

#2 Post by penpen » 17 Mar 2018 07:39

I'm unsure if i understand your task right:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
CALL :FUNCTION "VARIABLE"
CALL :FUNCTION "ALMOND"
CALL :FUNCTION "ARYA"
CALL :FUNCTION "CHEVA"
CALL :FUNCTION "SATYA"
CALL :FUNCTION "P"
goto :eof

:FUNCTION
if /I "%~1" lss "p" goto :oneThing
if /I "%~1" geq "q" goto :anotherThing
echo("%~1": Not branched.
goto :eof

:oneThing
echo("%~1": Do one thing.
goto :eof

:anotherThing
echo("%~1": Do another thing.
goto :eof
penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Branched Operate Based on Variable First Letter

#3 Post by Samir » 17 Mar 2018 16:48

penpen wrote:
17 Mar 2018 07:39
I'm unsure if i understand your task right:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
CALL :FUNCTION "VARIABLE"
CALL :FUNCTION "ALMOND"
CALL :FUNCTION "ARYA"
CALL :FUNCTION "CHEVA"
CALL :FUNCTION "SATYA"
CALL :FUNCTION "P"
goto :eof

:FUNCTION
if /I "%~1" lss "p" goto :oneThing
if /I "%~1" geq "q" goto :anotherThing
echo("%~1": Not branched.
goto :eof

:oneThing
echo("%~1": Do one thing.
goto :eof

:anotherThing
echo("%~1": Do another thing.
goto :eof
penpen
You nailed it penpen. 8)

If I would have known that lss operated like that on variables, I would have tried it. But I was with the impression that it only worked on numerics and text would have to be done as a string compare. I learned something new. :mrgreen:

Post Reply