Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBScript & INI files

270 views
Skip to first unread message

Yuri Weinstein (HotMail)

unread,
May 6, 2004, 10:24:17 PM5/6/04
to
I am just wondering ... does VBScript have any methods supporting INI files
structure?

Thx
YuriW


Torgeir Bakken (MVP)

unread,
May 6, 2004, 10:50:02 PM5/6/04
to
Yuri Weinstein (HotMail) wrote:
> I am just wondering ... does VBScript have any methods supporting
> INI files structure?
Hi

Sadly, no.


--
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/community/scriptcenter/default.mspx

MStrider

unread,
May 7, 2004, 3:56:59 AM5/7/04
to
why not? if you write the functions to deal with ini files as you would in
VB - would this not work?


"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message
news:enIGC59M...@TK2MSFTNGP09.phx.gbl...

Torgeir Bakken (MVP)

unread,
May 7, 2004, 4:52:27 AM5/7/04
to
MStrider wrote:

> why not? if you write the functions to deal with ini files as you would in
> VB - would this not work?

Hi

I think you will need to use the APIs GetPrivateProfileString and
PutPrivateProfileString from VB, but with VBScript, you can't call
APIs directly like you can in VB.

There exists free components out there that wraps these APIs in a
COM object so you can use it from a VBScript, e.g. this one:

AutoIt/AutoItX
http://www.hiddensoft.com/autoit3/index.php

AutoItX.dll is an ActiveX control version of AutoIt and
can be used from e.g. VBScript/Jscript.


You can of course use the FileSystemObject to handle INI files
as text files), for reading that is fine, but for writing you
should be sure that no other process is also trying to update
the INI file while your script is locking the file.

FileSystemObject example in this link, see the ReadIni function
and the WriteIni sub:
http://groups.google.com/groups?selm=3E8240AB.98EACD54%40hydro.com

Yuri Weinstein (HotMail)

unread,
May 7, 2004, 1:07:01 PM5/7/04
to
Great thx!


"Torgeir Bakken (MVP)" <

Torgeir.B...@hydro.com> wrote in message
news:O%23GSlDBN...@tk2msftngp13.phx.gbl...

Alan van Wyk

unread,
May 18, 2004, 10:03:23 AM5/18/04
to
Here is a function for reading ini files. I got it off the web
somewhere and it works perfectly. I have used it many many times.

'********To read ini File*****
'File = "c:\progs.ini"
'Section = "TROIKA"
'Item = "Version"
'TEST = ReadIni(FILE,SECTION,ITEM)
'msgbox TEST


Function ReadIni( file, section, item )
ReadIni = ""
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
If FileSysObj.FileExists( file ) Then
Set ini = FileSysObj.OpenTextFile( file, 1, False)

Do While ini.AtEndOfStream = False
line = ini.ReadLine

If line = "[" & section & "]" Then
line = ini.ReadLine
Do While Left( line, 1) <> "["
If InStr( line, item & "=" ) = 1 Then
ReadIni = mid( line, Len( item ) + 2 )
Exit Do
End If

If ini.AtEndOfStream Then Exit Do
line = ini.ReadLine
Loop
Exit Do
End If
Loop
ini.Close
End If
End Function


'####################################################################
'####################################################################
'####################################################################
'####################################################################
'####################################################################
'####################################################################
'####################################################################

To modify an ini file:

'********To Write ini File********
'call WRITEINI (File,section,item,value)

' WRITEINI ( file, section, item, value )
' file = path and name of ini file
' section = [Section] must be in brackets
' item = the variable to read; the item must be immediately followed
by =
'with no spaces.
' value = the value to assign to the item.

Sub WriteIni( file, section, item, value )

in_section = False
section_exists = False
item_exists = ( ReadIni( file, section, item ) <> "" )
wrote = False
path = Mid( file, 1, InStrRev( file, "\" ) )
Set fso = CreateObject("Scripting.FileSystemObject")
Set read_ini = fso.OpenTextFile( file, 1, True, TristateFalse )
Set write_ini = fso.CreateTextFile( path & "temp_ini.ini", False )

While read_ini.AtEndOfStream = False
line = read_ini.ReadLine

If wrote = False Then
If line = "[" & section & "]" Then
section_exists = True
in_section = True

ElseIf InStr( line, "[" ) = 1 Then
in_section = False
End If
End If

If in_section Then
If item_exists = False Then
write_ini.WriteLine line
write_ini.WriteLine item & "=" & value
wrote = True
in_section = False

ElseIf InStr( line, item & "=" ) = 1 Then
write_ini.WriteLine item & "=" & value
wrote = True
in_section = False

Else
write_ini.WriteLine line
End If
Else
write_ini.WriteLine line
End If
Wend

If section_exists = False Then ' section doesn't exist
write_ini.WriteLine
write_ini.WriteLine "[" & section & "]"
write_ini.WriteLine item & "=" & value
End If

read_ini.Close
write_ini.Close
fso.DeleteFile file
fso.MoveFile path & "temp_ini.ini", file
End Sub


'===========================================================
Function ReadIni( file, section, item )
ReadIni = ""
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
If FileSysObj.FileExists( file ) Then
Set ini = FileSysObj.OpenTextFile( file, 1, False)

Do While ini.AtEndOfStream = False
line = ini.ReadLine

If line = "[" & section & "]" Then
line = ini.ReadLine
Do While Left( line, 1) <> "["
If InStr( line, item & "=" ) = 1 Then
ReadIni = mid( line, Len( item ) + 2 )
Exit Do
End If

If ini.AtEndOfStream Then Exit Do
line = ini.ReadLine
Loop
Exit Do
End If
Loop
ini.Close
End If
End Function


'*****************************************


Easy as that!

Good Luck, hope it helps
Alan van Wyk

0 new messages