INI-Datei

Neben der Registry bietet auch eine INI-Datei, d. h. eine strukturierte Textdatei, die Möglichkeit Werte abzuspeichern und wieder auszulesen.

Werte aus einer INI-Datei lesen

  1. Function ReadIni( myFilePath, mySection, myKey )
  2. ' www.robvanderwoude.com/vbstech_files_ini.php
  3. ' This function returns a value read from an INI file
  4. '
  5. ' Arguments:
  6. ' myFilePath [string] the (path and) file name of the INI file
  7. ' mySection [string] the section in the INI file to be searched
  8. ' myKey [string] the key whose value is to be returned
  9. '
  10. ' Returns:
  11. ' the [string] value for the specified key in the specified section
  12. '
  13. ' CAVEAT: Will return a space if key exists but value is blank
  14. '
  15. ' Written by Keith Lacelle
  16. ' Modified by Denis St-Pierre and Rob van der Woude
  17.  
  18. Const ForReading = 1
  19. Const ForWriting = 2
  20. Const ForAppending = 8
  21.  
  22. Dim intEqualPos
  23. Dim objFSO, objIniFile
  24. Dim strFilePath, strKey, strLeftString, strLine, strSection
  25.  
  26. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  27.  
  28. ReadIni = ""
  29. strFilePath = Trim( myFilePath )
  30. strSection = Trim( mySection )
  31. strKey = Trim( myKey )
  32.  
  33. If objFSO.FileExists( strFilePath ) Then
  34. Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
  35. Do While objIniFile.AtEndOfStream = False
  36. strLine = Trim( objIniFile.ReadLine )
  37.  
  38. ' Check if section is found in the current line
  39. If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
  40. strLine = Trim( objIniFile.ReadLine )
  41.  
  42. ' Parse lines until the next section is reached
  43. Do While Left( strLine, 1 ) <> "["
  44. ' Find position of equal sign in the line
  45. intEqualPos = InStr( 1, strLine, "=", 1 )
  46. If intEqualPos > 0 Then
  47. strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
  48. ' Check if item is found in the current line
  49. If LCase( strLeftString ) = LCase( strKey ) Then
  50. ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
  51. ' In case the item exists but value is blank
  52. If ReadIni = "" Then
  53. ReadIni = " "
  54. End If
  55. ' Abort loop when item is found
  56. Exit Do
  57. End If
  58. End If
  59.  
  60. ' Abort if the end of the INI file is reached
  61. If objIniFile.AtEndOfStream Then Exit Do
  62.  
  63. ' Continue with next line
  64. strLine = Trim( objIniFile.ReadLine )
  65. Loop
  66. Exit Do
  67. End If
  68. Loop
  69. objIniFile.Close
  70. End If
  71. End Function

Hinweis: Die Datei wird als ASCII geöffnet. Um die Datei zwingen als ASCII- oder Unicode-Datei oder entsprechend dem Systemstandard zu öffnen, kann OpenTextFile - wie hier beschrieben - um einen weiteren Parameter ergänzt werden.

Werte in eine INI-Datei schreiben

  1. Sub WriteIni( myFilePath, mySection, myKey, myValue )
  2. ' http://www.robvanderwoude.com/vbstech_files_ini.php
  3. ' This subroutine writes a value to an INI file
  4. '
  5. ' Arguments:
  6. ' myFilePath [string] the (path and) file name of the INI file
  7. ' mySection [string] the section in the INI file to be searched
  8. ' myKey [string] the key whose value is to be written
  9. ' myValue [string] the value to be written (myKey will be
  10. ' deleted if myValue is <DELETE_THIS_VALUE>)
  11. '
  12. ' Returns:
  13. ' N/A
  14. '
  15. ' CAVEAT: WriteIni function needs ReadIni function to run
  16. '
  17. 'To delete a key in an INI file, use WriteINI with a value "<DELETE_THIS_VALUE>".
  18. '
  19. ' Written by Keith Lacelle
  20. ' Modified by Denis St-Pierre, Johan Pol and Rob van der Woude
  21.  
  22. Const ForReading = 1
  23. Const ForWriting = 2
  24. Const ForAppending = 8
  25.  
  26. Dim blnInSection, blnKeyExists, blnSectionExists, blnWritten
  27. Dim intEqualPos
  28. Dim objFSO, objNewIni, objOrgIni, wshShell
  29. Dim strFilePath, strFolderPath, strKey, strLeftString
  30. Dim strLine, strSection, strTempDir, strTempFile, strValue
  31.  
  32. strFilePath = Trim( myFilePath )
  33. strSection = Trim( mySection )
  34. strKey = Trim( myKey )
  35. strValue = Trim( myValue )
  36.  
  37. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  38. Set wshShell = CreateObject( "WScript.Shell" )
  39.  
  40. strTempDir = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
  41. strTempFile = objFSO.BuildPath( strTempDir, objFSO.GetTempName )
  42.  
  43. Set objOrgIni = objFSO.OpenTextFile( strFilePath, ForReading, True )
  44. Set objNewIni = objFSO.CreateTextFile( strTempFile, False, False )
  45.  
  46. blnInSection = False
  47. blnSectionExists = False
  48. ' Check if the specified key already exists
  49. blnKeyExists = ( ReadIni( strFilePath, strSection, strKey ) <> "" )
  50. blnWritten = False
  51.  
  52. ' Check if path to INI file exists, quit if not
  53. strFolderPath = Mid( strFilePath, 1, InStrRev( strFilePath, "\" ) )
  54. If Not objFSO.FolderExists ( strFolderPath ) Then
  55. WScript.Echo "Error: WriteIni failed, folder path (" _
  56. & strFolderPath & ") to ini file " _
  57. & strFilePath & " not found!"
  58. Set objOrgIni = Nothing
  59. Set objNewIni = Nothing
  60. Set objFSO = Nothing
  61. WScript.Quit 1
  62. End If
  63.  
  64. While objOrgIni.AtEndOfStream = False
  65. strLine = Trim( objOrgIni.ReadLine )
  66. If blnWritten = False Then
  67. If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
  68. blnSectionExists = True
  69. blnInSection = True
  70. ElseIf InStr( strLine, "[" ) = 1 Then
  71. blnInSection = False
  72. End If
  73. End If
  74.  
  75. If blnInSection Then
  76. If blnKeyExists Then
  77. intEqualPos = InStr( 1, strLine, "=", vbTextCompare )
  78. If intEqualPos > 0 Then
  79. strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
  80. If LCase( strLeftString ) = LCase( strKey ) Then
  81. ' Only write the key if the value isn't empty
  82. ' Modification by Johan Pol
  83. If strValue <> "<DELETE_THIS_VALUE>" Then
  84. objNewIni.WriteLine strKey & "=" & strValue
  85. End If
  86. blnWritten = True
  87. blnInSection = False
  88. End If
  89. End If
  90. If Not blnWritten Then
  91. objNewIni.WriteLine strLine
  92. End If
  93. Else
  94. objNewIni.WriteLine strLine
  95. ' Only write the key if the value isn't empty
  96. ' Modification by Johan Pol
  97. If strValue <> "<DELETE_THIS_VALUE>" Then
  98. objNewIni.WriteLine strKey & "=" & strValue
  99. End If
  100. blnWritten = True
  101. blnInSection = False
  102. End If
  103. Else
  104. objNewIni.WriteLine strLine
  105. End If
  106. Wend
  107.  
  108. If blnSectionExists = False Then ' section doesn't exist
  109. objNewIni.WriteLine
  110. objNewIni.WriteLine "[" & strSection & "]"
  111. ' Only write the key if the value isn't empty
  112. ' Modification by Johan Pol
  113. If strValue <> "<DELETE_THIS_VALUE>" Then
  114. objNewIni.WriteLine strKey & "=" & strValue
  115. End If
  116. End If
  117.  
  118. objOrgIni.Close
  119. objNewIni.Close
  120.  
  121. ' Delete old INI file
  122. objFSO.DeleteFile strFilePath, True
  123. ' Rename new INI file
  124. objFSO.MoveFile strTempFile, strFilePath
  125.  
  126. Set objOrgIni = Nothing
  127. Set objNewIni = Nothing
  128. Set objFSO = Nothing
  129. Set wshShell = Nothing
  130. End Sub

Beispiel:

  1. 'FFSubmenu=Test
  2. 'FFName=INI bearbeiten
  3.  
  4. ' die Datei test.ini muss bereits existieren!
  5. WriteIni "D:\1\test.ini", "Section1", "Key1", "2000" ' einen Schlüssel anlegen und Wert schreiben
  6. WriteIni "D:\1\test.ini", "Section1", "Key2", "<DELETE_THIS_VALUE>" ' einen Schlüssel löschen
  7. Msgbox ReadIni ("D:\1\test.ini", "Section1", "Key1") ' eine Wert auslesen

INI-Section expotieren

  1. Sub INISectionExport (inifile, section, exportfile)
  2. ' kopiert eine INI-Section mit allen Untereinträgen in eine Datei
  3. ' der bisherige Inhalt der Datei wird überschrieben
  4. Const ForReading = 1, ForWriting = 2
  5. Dim fso, f1, f2
  6. Dim saveline, iniarray, i
  7. Set fso = CreateObject("Scripting.FileSystemObject")
  8. Set f1 = fso.OpenTextFile(inifile, ForReading)
  9. If fso.FileExists(exportfile) Then
  10. Set f2 = fso.OpenTextFile(exportfile, ForWriting)
  11. Else
  12. fso.CreateTextFile exportfile, True
  13. Set f2 = fso.OpenTextFile(exportfile, ForWriting)
  14. End If
  15. iniarray = Split(f1.ReadAll, vbNewLine)
  16. saveline = 0
  17. For i = 0 to UBound(iniarray)
  18. If InStr(iniarray(i), section) Then saveline = 1 ' Sektion beginnt
  19. If InStr(iniarray(i), "[") and Not InStr(iniarray(i), section) Then saveline = 0 ' Sektion endet
  20. If saveline = 1 Then
  21. f2.WriteLine iniarray(i)
  22. End If
  23. Next
  24. f1.Close
  25. f2.Close
  26. Set f2 = Nothing
  27. Set f1 = Nothing
  28. End Sub

INI-Section importieren

  1. Sub INISectionImport (inifile, importfile)
  2. ' importiert eine INI-Section aus eine Datei
  3. ' es wird nicht geprüft, ob eine gleichnamige Section bereits vorhanden ist
  4. Const ForReading = 1, ForAppending = 8
  5. Dim fso, f1, f2
  6. Set fso = CreateObject("Scripting.FileSystemObject")
  7. Set f1 = fso.OpenTextFile(inifile, ForAppending)
  8. Set f2 = fso.OpenTextFile(importfile, ForReading)
  9. Do While f2.AtEndOfStream <> True
  10. f1.WriteLine f2.ReadLine
  11. Loop
  12. f1.Close
  13. f2.Close
  14. Set f1 = Nothing
  15. Set f2 = Nothing
  16. End Sub

INI-Section löschen

  1. Sub INISectionDelet (inifile, section)
  2. ' löscht eine INI-Section mit allen Untereinträgen
  3. Const ForReading = 1, ForWriting = 2
  4. Dim fso, f1
  5. Dim delline, iniarray, i
  6. Set fso = CreateObject("Scripting.FileSystemObject")
  7. Set f1 = fso.OpenTextFile(inifile, ForReading)
  8. iniarray = Split(f1.ReadAll, vbNewLine)
  9. For i = 0 to UBound(iniarray)
  10. If InStr(iniarray(i), section) Then delline = 1 ' Sektion beginnt
  11. If InStr(iniarray(i), "[") and Not InStr(iniarray(i), section) Then delline = 0 ' Sektion endet
  12. If delline = 1 Then
  13. iniarray(i) = "##delline**"
  14. End If
  15. Next
  16. Set f1 = fso.OpenTextFile(inifile, ForWriting)
  17. For i = 0 to UBound(iniarray)
  18. If iniarray(i) <> "##delline**" Then
  19. f1.WriteLine iniarray(i)
  20. End If
  21. Next
  22. f1.Close
  23. Set f1 = Nothing
  24. End Sub