I need a way to read/write an INI file from a .vbs file
without using ActiveX.

Re: INI Files by Torgeir

Torgeir
Fri Mar 11 16:07:51 CST 2005

Anon wrote:

> I need a way to read/write an INI file from a .vbs file
> without using ActiveX.
Hi

For a VBScript ini read/write example using the
Scripting.FileSystemObject, you can take a look here:

From: Torgeir Bakken (Torgeir.Bakken-spam@hydro.com)
Subject: Re: Change text in line three
Newsgroups: microsoft.public.scripting.wsh
Date: 2002-09-11 11:53:18 PST
http://groups.google.com/groups?selm=3D7F8F89.F09B18B1%40hydro.com


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: INI Files by Anon

Anon
Fri Mar 11 16:23:51 CST 2005

>Hi
>
>For a VBScript ini read/write example using the
>Scripting.FileSystemObject, you can take a look here:
>
>From: Torgeir Bakken (Torgeir.Bakken-spam@hydro.com)
>Subject: Re: Change text in line three
>Newsgroups: microsoft.public.scripting.wsh
>Date: 2002-09-11 11:53:18 PST
>http://groups.google.com/groups?selm=3D7F8F89.F09B18B1%
40hydro.com
>
>
>--
>torgeir, Microsoft MVP Scripting and WMI, Porsgrunn
Norway
>Administration scripting examples and an ONLINE version
of
>the 1328 page Scripting Guide:
>http://www.microsoft.com/technet/scriptcenter/default.msp
x
>.
>

I will look through all this and see what works. It
looks like it is the ticket, a little more lengthy than
what I'm hoping for, but I guess VBScript don't have any
built in function for this.

Thanks

Re: INI Files by de

de
Fri Mar 11 21:40:18 CST 2005

Anything here good for you? BTW - People ask me why have the trailing
quotes in comments. It's because my editor shows commented lines as black
text on a light grey background and thr trailing quote keeps the line from
being blank trimmed on file save (which would display a nasty ragged right
edge). My desk may be messy but my code is neat.

set ini = New IniFile

ini.Read("c:\vista.ini")
ini.Dump

Display ini,"[reports]","EXE"


Sub Display ( ini , section , variable )

dim value: value = ini.GetValue(section,variable)

if isNull(value) then
wscript.echo variable,"is not defined in",section
else
wscript.echo section,variable,"=",value
end if

End Sub

'
'
' Name:
'
'
'
' IniFile.cls
'
'
'
' Description:
'
'
'
' This class implements a basic ini file interface. Using this class you
can read and '
' parse an ini file. The methods give easy access to the variables by
section. '
'
'
' Ini File Format
'
'
'
' An ini file contains variables that are defined in sections. A section
is defined by '
' entering [sectionname] on a line by itself where sectionname is any
name you want to '
' use (when you specift the section name in GetValue, include the [ and ]
characters). '
' Any variables that are defined prior to the start of a section are put
into the default '
' section which is named "".
'
'
'
' A variable is defined by creating a line with variable=value. You can
include quotes or '
' not as your choice. The quotes will not get stored as part of the
value. '
'
'
' Section and variable names are case-insensitive.
'
'
'
' Properties:
'
'
'
' none
'
'
'
' Methods:
'
'
'
' Read(filename)
'
'
'
' Reads and parses the ini file with the given name. Returns true if
the file exists, '
' false otherwise. Any ini file entry that does not begin with "[" (a
section name) or '
' contain a variable=value pair is ignored.
'
'
'
' GetValue(section,variable)
'
'
'
' Returns the value of the variable in the given section. If the
section or variable '
' is not defined then the returned value is null (not the null string,
but null). '
'
'
' Dump
'
'
'
' Returns the parsed contents of the ini file as a long string with
lines terminated '
' by vbCrLf.
'
'
'
' Implementation:
'
'
'
' Each section (including the default section) is stored as a dictionary
entry. Each '
' entry in this dictionary is itself a dictionary where each entry
contains a variable '
' and value. The variable is the key and the value is the contents of the
location with '
' that key.
'
'
'
' Future:
'
'
'
' I may add a Write method to allow updates to the ini file. This will
require some fancy '
' coding to preserve comments.
'
'
'
' Audit:
'
'
'
' 2005-03-11 jdeg original code
'
'
'

Class IniFile

Private m_ini

'
'
' Create the root dictionary and a default (no name section
'
'
'

Private Sub Class_Initialize ()

set m_ini = CreateObject("Scripting.Dictionary")
m_ini.CompareMode = vbTextCompare
set m_ini("") = CreateObject("Scripting.Dictionary")

End Sub

'
'
' Delete all variable and section entries then delete the dictionary
'
'
'

Private Sub Class_Terminate ()

dim entry

for each entry in m_ini.Keys
m_ini(entry).RemoveAll
set m_ini(entry) = Nothing
next

m_ini.RemoveAll
set m_ini = Nothing

End Sub

'
'
' Return the value of the variable in the given section (or null if not
found) '
'
'

Public Function GetValue ( section , variable )

GetValue = null

if not m_ini.Exists(section) then exit Function
if not m_ini(section).Exists(variable) then exit function

GetValue = m_ini(section)(variable)

End Function

'
'
' Read the ini file into the dictionary. Return True if there were no
errors. '
'
'

Public Function Read ( inifileName )

dim fso 'file system object for file access
'
dim all 'all of the cntents of the ini file
'
dim var 'the variable name from variable=value
'
dim val 'the variable value from variable=value
'
dim section 'the current section name
'
dim line 'the current line in the ini file
'

set fso = CreateObject("Scripting.FileSystemObject")

if fso.FileExists(inifileName) then

'read the entire contents of the ini file

all = fso.OpenTextFile(inifileName).ReadAll
all = Split(all,vbCrLf)

'start wiht the default section

section = ""

for each line in all

line = Trim(line)

'get line type and parse

if not Comment(line,var,val) then

'not a comment - add section (maybe) or variable

if left(line,1) = "[" then
section = line
if not m_ini.Exists(section) then
set m_ini(section) =
CreateObject("Scripting.Dictionary")
m_ini(section).CompareMode = vbtextCompare
end if
else
m_ini(section)(var) = val
end if

end if

next

Read = True

else

Read = False

End if

End Function

'
'
' Returns the entire contents of the parsed ini file as a string with
lines terminated by '
' vbCrLf.
'
'
'

Public Function Dump ()

dim section
dim variable

Dump = ""

for each section in m_ini.Keys

Dump = Dump & section & vbCrLf

for each variable in m_ini(section).Keys
Dump = Dump & " " & variable & " = " &
m_ini(section)(variable)
next

next

End Function

'
'
' Returns True if the line is a comment. A line is considered to be a
comment if '
'
'
' 1) It is empty
'
' 2) It starts with a semi-colon
'
' 3) It is not a section or variable definition
'
'
'
' If the line is a variable definition, the variable name and value are
parsed and '
' returned. Quotes are removed from the value if appearing in the first
or last position '
'
'

Private Function Comment ( line , var , val )

Comment = True

if Len(line) = 0 then Exit Function
if Left(line,1) = ";" then Exit Function
if instr(line,"=") = 0 and left(line,1) <> "[" then Exit Function

Comment = False

if left(line,1) <> "[" then
var = Trim(Left(line,Instr(line,"=")-1))
val = Trim(Mid(line, Instr(line,"=")+1))
if Left(val,1) = """" then val = Mid(val,2)
if Right(val,1) = """" then val = Mid(val,1,Len(val)-1)
end if

End Function

End Class



Re: INI Files by Makker

Makker
Thu Apr 07 10:21:17 CDT 2005

I always use this code, and it works absolutely perfect:

http://www.motobit.com/tips/detpg_asp-vbs-read-write-ini-files/

It's less shorter too...

"Anon" <anonymous@discussions.microsoft.com> schreef in bericht
news:7be301c52686$d1604e70$a401280a@phx.gbl...
>I need a way to read/write an INI file from a .vbs file
> without using ActiveX.