Ini

A generic class to access ini files

Just put thi class in your scripts and use it like this :

Dim x
set x=new ini
x.FileName="C:\WINDOWS\Bureau\ini\wanadoo.ini"
x.Section="icone0"
x.Key="Id"
MsgBox x.Value
x.Key="TestKey"
x.Value="TestValue"
MsgBox x.Value
Set x=Nothing

File Name : class_ini.vbs
Requirement : WSH 5.1
Author : Jean-Luc Antoine
Submitted : 26/08/2001
Category : Class

To use a class, follow this syntax :
Dim MyObjClass
set MyObjClass=new MyClass
MyObjClass.Property="Any value"
MsgBox MyObjClass.AnotherProperty
MyObjClass.MyMethod MyParam1,MyParam2
Set MyObjClass=Nothing

Very simple, no need to rename any variable nor register any ActiveX.

Option Explicit
Class Ini
	Private fso,f

	Public Filename	'Path to the ini File
	Public Section	'[section]
	Public Key	'Key=Value
	Public Default	'Return it when an error occurs

	Private Sub Class_Initialize   ' Setup Initialize event.
		Default=""
		Set fso=CreateObject("Scripting.FileSystemObject")
	End Sub
	Private Sub Class_Terminate   ' Setup Terminate event.
		Set fso=Nothing
	End Sub

	Property Get Content()
		'All the file in a string
		If fso.FileExists(FileName) Then
			Set f=fso.OpenTextFile(FileName,1)
			Content=f.ReadAll
			f.close
			Set f=Nothing
		Else
			Content=""
		End If
	End Property
	Property Let Content(sContent)
		'Create a brand new ini file
		Set f=fso.CreateTextFile(FileName,True)
		f.Write sContent
		f.close
		Set f=Nothing
	End Property

	Property Get ContentArray()
		'All the file in an array of lines
		ContentArray=Split(Content,vbCrLf,-1,1)
	End Property

	Private Sub FindSection(ByRef StartLine, ByRef EndLine)
		Dim x,A,s
		StartLine=-1
		EndLine=-2
		A=ContentArray
		For x=0 To UBound(A)
			s=UCase(Trim(A(x)))
			If s="[" & UCase(section) & "]" Then
				StartLine=x
			Else
				If (Left(s,1)="[") And (Right(s,1)="]") Then
					If StartLine>=0 Then
						EndLine=x-1
						If EndLine>0 Then 'A Space before the next section ?
							If Trim(A(EndLine))="" Then EndLine=EndLine-1
						End If
						Exit Sub
					End If
				End If
			End If
		Next
		If (StartLine>=0) And (EndLine<0) Then EndLine=UBound(A)
	End Sub

	Property Get Value()
		'Retrieve the value for the current key in the current section
		Dim x,i,j,A,s
		FindSection i,j
		A=ContentArray
		Value=Default
		'Search only in the good section
		For x=i+1 To j
			s=Trim(A(x))
			If UCase(Left(s,Len(Key)))=UCase(Key) Then
				Select Case Mid(s,Len(Key)+1,1)
				Case "="
					Value=Trim(Mid(s,Len(Key)+2))
					Exit Property
				Case " ",chr(9)
					x=Instr(Len(Key),s,"=")
					Value=Trim(Mid(s,x+1))
					Exit Property
				End Select
			End If
		Next
	End Property
	Property Let Value(sValue)
		' Write the value for a key in a section
		Dim i,j,A,x,s,f
		FindSection i,j
		If i<0 Then 'Session doesn't exist
			Content=Content & vbCrLf & "[" & section & "]" & vbCrLf & Key & "=" & sValue
		Else
			A=ContentArray
			f=-1
			'Search for the key, either the key exists or not
			For x=i+1 To j
				s=Trim(A(x))
				If UCase(Left(s,Len(Key)))=UCase(Key) Then
					Select Case Mid(s,Len(Key)+1,1)
					Case " ",chr(9),"="
						f=x	'Key found
						A(x)=Key & "=" & sValue
					End Select
				End If
			Next
			If f=-1 Then
				'Not found, add it at the end of the section
				Redim Preserve A(UBound(A)+1)
				For x=UBound(A) To j+2 Step -1
					A(x)=A(x-1)
				Next
				A(j+1)=Key & "=" & sValue
			End If
			'Define the content
			s=""
			For x=0 To UBound(A)	
				s=s & A(x) & vbCrLf
			Next
			'Suppress the last CRLF
			If Right(s,2)=vbCrLf Then s=Left(s,Len(s)-2)
			Content=s	'Write it
		End If
	End Property
End Class 
Server Info
Remote Info
Compress

Batchs
BootSector
Droit
Accessoire GFA
On Now
Ecran plasma
Système d'exploitation
Son
Unités
Scripting
Multilingue
Site Web
Bruit et son
Architecture Google


4K
Color picker
TaskList
Backup files
Mini Port Scanner
Website To CHM
PixyDemo
Web password recovery
4KWebServer
Kill Popup
VBE decoder
Script Optimizer
Database Password Recovery
Class
Ini
EMail validator
Graphical Class
MP3 Info
Pinger Class
IP ranger class
SoftwareMetering
Path Validator
Time Stamper Class
LoggerCLS
HTA
Keyword Ranking
DOM Explorer
HTA-Notepad
PixyPortManager
Other
flashMessage
Kill Popup
VBE decoder (fixed)
Error Codes
Maze generator
Char counter
HTML To Word
ICQ Choose User
ScreenSaver Password decoder
Mp3Playlister - singleList
Long Filename To Short
Trace Math
Dump Hexa File
Self modifying script
Mp3Playlister - multiList
HTANoid
PixyWebServer
Python
Progress indication

©2002 Jean-Luc Antoine. All Rights Reserved. Scripts or any other material on this website may not be redistributed or put as part of ANY collection (script archives, CDs etc) without prior written permission. Permission granted to use and modify any of the scripts found on InterClasse.com