1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| Set fso = CreateObject("Scripting.FileSystemObject")
Set wshell = CreateObject("WScript.Shell")
Set oFile = fso.GetFile("c:\temp\Test\parametre.ini")
NewData = "V2 - 1.17"
'ici pour lire la valeur de la cle Version sous la Section InformationFramework
vif = WriteReadIni(oFile,"InformationFramework","Version",Null)
wscript.echo "Version = "& vif
If vif = "V2 - 1.16" Then
WScript.Echo "version présente"
Else
WScript.Echo "version non présente"
WScript.Echo "La valeur a ete remplace : "& WriteReadIni(oFile,"InformationFramework","Version",NewData)
vif = WriteReadIni(oFile,"InformationFramework","Version",Null)
wscript.echo "La valeur de la cle Version a ete mis a jour = "& vif
End If
'-------------------------------------------------------------------------------------------------------------------------------------------
Function WriteReadIni(oFile,section,key,value)
' écrit/lit la clé <key> de section <section> de l'objet fichier oFile avec la valeur <value> si lecture : value = Null
' en écriture si la section et/ou la clé n'existent pas, elles seront créées
' en écriture renvoie toujours vrai, en lecture renvoie soit : la valeur de clé, une chaine vide en cas de clé vide ou Faux si la clé n'existe pas
Dim oText,iniText,sectText,newSectText,keyText
' Initialisation des objets regexp
' peut être déplacé dans le code principal en cas d'appels successifs
Set reg = New RegExp
Set regSub = New RegExp
reg.IgnoreCase = True
reg.MultiLine=True
regSub.IgnoreCase = True
' lecture du fichier ini et fermeture
Set oText = oFile.OpenAsTextStream(1,0)
iniText = oText.ReadAll
oText.Close
'***************************************************
reg.Global=True
reg.Pattern = "^\[(.+)\]"
Set Matches=reg.Execute(iniText)
iC=Matches.Count
For Each Match In Matches
iC=iC-1
If Match.SubMatches(0)=section Then Exit For
Next
reg.Global=false
reg.Pattern = "^\[" & section & "\]((.|\n)+)((\n\[(.|\n)+){"&iC&"})" ' quid pattern statique?
'***************************************************
regSub.Pattern = "\b" & key & " *= *([^;\f\n\r\t\v]*)" 'pas de clé multiline
On Error Resume Next
If IsNull(value) Then ' lecture clé
Set Matches=reg.Execute(iniText)
WriteReadIni = regSub.Execute(reg.Execute(iniText).Item(0).SubMatches(0)).Item(0).SubMatches(0)
If Err.Number = 5 then WriteReadIni = False
Else ' écriture clé
sectText = reg.Execute(iniText).Item(0).SubMatches(0)
nextText = reg.Execute(iniText).Item(0).SubMatches(2) ' le reste du fichier après la section trouvée ->conseq submatch
If Err.Number = 5 Then ' section inconnue elle est ajoutée en fin de fichier
iniText = iniText & vbCrLf & "[" & section & "]" & vbCrLf & key & "=" & value
Else ' la section existe on tente de remplacer la clé
newSectText = regSub.Replace(sectText,key & "=" & value)
If newSectText = sectText Then ' pas de remplacement opéré, la clé n'existe pas donc on l'ajoute en fin de section
If Right(sectText,1) = vbCr Then keyText = key & "=" & value Else keyText = vbCrLf & key & "=" & value
newSectText = sectText & keyText
End If
' remplacement de la section ancienne par la section maj
iniText = reg.Replace(iniText,"[" & section & "]" & newSectText & nextText)
End If
' msgbox iniText
' écriture du fichier ini mis à jour
Set oText = oFile.OpenAsTextStream(2,0)
oText.Write iniText
oText.Close
WriteReadIni = True
End If
End Function
'------------------------------------------------------------------------------------------------------------------------------------------- |