Lire ecrire dans un fichier ini

Function ReadINI(file, section, key)

Set FileSysObj = CreateObject( Scripting.FileSystemObject )

ReadIni=

If FileSysObj.FileExists(file) then

Set ini = FileSysObj.OpenTextFile( file, 1, False)

If section= then

set reSection =new RegExp

reSection.Global =True

reSection.IgnoreCase=True

reSection.Pattern = [([a-zA-Z0-9 ])]

Do While ini.AtEndofStream =False

line = ini.ReadLine

if reSection.Test(line) then

tempSection=tempSection & reSection.Replace(line, $1 ) & ,

end if

loop

ini.close

tempSection=left(tempSection, len(tempSection)-1)

ReadINI=split(tempSection, , )

set reSection=nothing

exit function

end if

If key= then

set reSection =new RegExp

reSection.Global =True

reSection.IgnoreCase=True

reSection.Pattern = s
[s* & section & s*]

Do While ini.AtEndofStream =False

line = ini.ReadLine

if reSection.Test(line) then

line=ini.ReadLine

do while instr(line, [ )=0

tempKeys=tempKeys & trim(left(line,instr(line, = )-1)) & ,

line=ini.ReadLine

loop

tempKeys=left(tempKeys,(len(tempkeys)-1))

ReadINI =split(tempKeys, , )

exit function

end if

loop

end if

Set reSection =new RegExp

reSection.Global =False

reSection.IgnoreCase=True

reSection.Pattern = s*[s* & section & s*]

Set reKey =new RegExp

reKey.Global =False

reKey.IgnoreCase=True

reKey.Pattern= s* & key & s*=s*

Do While ini.AtEndofStream = False

line = ini.ReadLine

if reSection.Test(line) = True then

line=ini.ReadLine

do while instr(line, [ )=0

if reKey.Test(line) then

ReadINI=trim(mid(line,instr(line, = )+1))

exit do

end if

line=ini.ReadLine

Loop

exit do

end if

loop

ini.Close

set reSection=nothing

set reKey =nothing

end if

end function

Sub WriteIni( file, section, item, value )

set FileSysObj = CreateObject( Scripting.FileSystemObject )

in_section = False

section_exists = False

item_exists = ( ReadIni( file, section, item ) <> )

wrote = False

path = Mid( file, 1, InStrRev( file, ) )

Set read_ini = FileSysObj.OpenTextFile( file, 1, True, TristateFalse )

Set write_ini = FileSysObj.CreateTextFile( path & temp_ini.ini , False )

Set reWSection =new RegExp

reWSection.Global =False

reWSection.IgnoreCase=True

reWSection.Pattern = s*[s* & section & s*]

Set reItem =new RegExp

reItem.Global =False

reItem.IgnoreCase=True

reItem.Pattern = s* & item & s*=

While read_ini.AtEndOfStream = False

line = read_ini.ReadLine

If wrote = False Then

If reWSection.Test(line) Then

section_exists = True

in_section = True

ElseIf InStr( line, [ )> 0 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

msgbox Writing & line

ElseIf reItem.Test(line) 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

section=trim(section)

item =trim(item)

write_ini.WriteLine

write_ini.WriteLine [ & section & ]

write_ini.WriteLine item & = & value

End If

read_ini.Close

write_ini.Close

FileSysObj.DeleteFile file

FileSysObj.MoveFile path & temp_ini.ini , file

set reWSection=nothing

set reItem=nothing

End Sub





[ This message was edited by: Chavers on 2002-07-17 13:08 ]