VB icon

Reading a Configuration File

Email
Submitted on: 1/5/2015 5:45:00 PM
By: Subodh Dash (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 2861
 
     This code shows how to 'read a configuration file and use 'the key-value pair. The configuration 'file must be in "key=value" format. '(Similar to a java property file)
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Reading a Configuration File
'**************************************
Free to use / free to distribute
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Reading a Configuration File
' Description:This code shows how to
'read a configuration file and use
'the key-value pair. The configuration
'file must be in "key=value" format.
'(Similar to a java property file)
' By: Subodh Dash (from psc cd)
'
' Inputs:configuration file name with full path
'
' Assumes:'The code must be modified to
'use the actual key variables
'from your configuration file.
'The code uses a sample of few keys.
'You must customize it to your needs.
'The property file may also have comments.
'The comment lines must start with #.
'**************************************

'Let the configuration file be in
'c:\myproject\myconfile.extension
'And the contents of the file is as follows
#Hash lines are comment lines
#You may add as many hash lines as you want
icsDrive=C
icsProjectDir=ICS
icsMinDiskSpace=5000
icsMaxDiskSpace=100000
icsMailApplication=Outlook.Application
icsEMailTo=mymail@yourhost.com
#End of the configuration file
'
'
'
Option Explicit
DIM FSO
SET FSO = CreateObject ("Scripting.FileSystemObject")
'Declare the variables to be used from the property file
 DIM icsDrive
 DIM icsProjectDir
 DIM icsMinDiskSpace
 DIM icsMaxDiskSpace
 DIM icsMailApplication
 DIM icsEMailTo
 Main
Sub Main
	CALL SetConfigFromFile("c:\myproject\myconfile.extension")
	msgbox icsDrive
	msgbox icsProjectDir
	msgbox icsMinDiskSpace
	msgbox icsMaxDiskSpace
	msgbox icsMailApplication
	msgbox icsEMailTo
End sub
'***Read Configuration File***
Sub SetConfigFromFile(fileName)
 DIM strConfigLine
 DIM fConFile
 DIM EqPos, strLen, varName, varVal
 SET fConFile = fso.OpenTextFile(fileName)
 WHILE NOT fConFile.AtEndOfStream
 	strConfigLine = fConFile.ReadLine
	strConfigLine = TRIM(strConfigLine)
	IF (INSTR(1,strConfigLine,"#",1) <> 1 AND LEN(strConfigLine) <> 0) THEN
		EqPos = INSTR(1,strConfigLine,"=",1)
		strLen = LEN(strConfigLine)
		varName = LCASE(TRIM(MID(strConfigLine, 1, EqPos - 1)))
		varVal = TRIM(MID(strConfigLine, EqPos + 1, strLen - EqPos))
		SELECT CASE varName
			'ADD EACH OCCURRENCE OF THE CONFIGURATION FILE VARIABLES(KEYS)
			CASE LCASE("icsDrive")
				IF varVal <> "" THEN icsDrive = varVal
			CASE LCASE("icsProjectDir")
				IF varVal <> "" THEN icsProjectDir = varVal
			CASE LCASE("icsMinDiskSpace")
				IF varVal <> "" THEN icsMinDiskSpace = varVal
			CASE LCASE("icsMaxDiskSpace")
				IF varVal <> "" THEN icsMaxDiskSpace = varVal
			CASE LCASE("icsMailApplication")
				IF varVal <> "" THEN icsMailApplication = varVal
			CASE LCASE("icsEMailTo")
				IF varVal <> "" THEN icsEMailTo = varVal
		END SELECT
	END IF
 WEND
 fConFile.Close
End Sub


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.