RS>Hello,
RS>Does anyone know how to pass more than 1 variable to COMMAND$? I am
RS>trying to trap DOS environment variables...here is my example:
Here's a function that works very well for handling just that sort of
problem. James Davis wrote the original version of this code; I've
added my own tidying up and what I think are better variable names for
improved understanding.
'------------ a short example program to show how to use this ----------
' here's the declaration for it
DECLARE FUNCTION GetNextWord(STRING, STRING) AS STRING
' then we declare the variables we'll need
DIM CommandString AS STRING ' a copy of the command line we got
DIM CmdDelimList AS STRING ' the delimiter list we'll use
' don't forget to set the Command$ string under Run/Command$
' try something like "fred -barney /wilma /-betty" as a string
CommandString = LTRIM$(RTRIM$(Command$))
CmdDelimList = " /-"
' and read through the command tail, one word at a time
WHILE LEN(CommandString) > 0
PRINT GetNextWord(CommandString, CmdDelimList)
WEND
END
' and the function itself
FUNCTION GetNextWord(ParseString AS STRING, DelimList AS STRING) LOCAL AS
STRING
'ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
¿
'³ This function returns the first word in a string, separated by characters
³
'³ in a delimiter list.
³
'³
³
'³ Note that this function will strip the word it returns from the original
³
'³ ParseString it received, so make a copy of the string to work on if you
³
'³ need to retain it for any reason.
³
'³
³
'³ -- special thanks to James Davis for the original code used in this one
³
'ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
Ù
DIM NextWord AS STRING ' the next word we're finding
' first, strip any delimiters from the beginning and end of the string
ParseString = LTRIM$(RTRIM$(ParseString), ANY DelimList)
' get everything from the beginning of the string up to the
' first delimiter or the end of the string, whichever comes first
NextWord = LEFT$(ParseString, 1) + _
EXTRACT$(MID$(ParseString, 2), ANY DelimList)
' if we haven't found the last word in the Parse string ...
IF LEN(NextWord) < LEN(ParseString) THEN
' remove the word we found
ParseString = MID$(ParseString, LEN(NextWord) + 1)
ELSE
' kill off the string
ParseString = ""
END IF
' and return our value
GetNextWord = NextWord
END FUNCTION
'------------------------ end short example ---------------------
Understand as well that this routine works very well to read any
"sentence" of words, not just a command line. I've shown how to use it
on the command line, only because that was your question. The most
common way it gets used in my programs is with a single space as the
delimiter, reading words from a block of text, one at a time.
Cheers.
-= Steve Legg -=- steve.legg@westonia.com =-
-= Oshawa, Ontario CANADA -=- leggs@mail.durham.net =-
---
* OLXWin 1.00b * Press any key to continue or any other key to quit
---------------
* Origin: Westonia Computer Systems 1:250/636 (416)241-1981 (1:3615/51)
|