Have a request for a topic in Code of the Week? Email us at request@codeoftheweek.com
Token substitution using environment variables.
This module contains a single routine that does a substitution of environment variables in the data passed to it. It is very useful for processing command lines in which the user wants to use data that is stored in the environment (such as a username). It can also be used in any custom configuration or scripting files your application might use.
An environment variable is usually set in the AUTOEXEC.BAT (or any other batch file) or a network login script. The DOS command to set an environment variable is SET. An example would be SET USER=david.
If you have any questions about using this class, let us know at questions@codeoftheweek.com
Public Function ParseEnvVar(ByVal sData As String) As String
This routine will take the data passed in sData and search for any embedded environment variables. As it finds each environment variable it will substitute the value for the environment variable into the string. Once it processes all environment variables it will scan the string for an "escaped" delimiter (two delimiters next to each other) and replace them with a single delimiter character.
The processed string if successful. It will raise an error if something "bad" happens.
Below is an example of how this function works. It assumes there is an environment variable called USER that is set to JOHN.
Debug.Print ParseEnvVar("The username is %user%.")
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0088.html