Original Post
Public Sub DeleteItem(ByVal group As String, ByVal item As String, Optional file As _
String)
Dim nFileNum As String, sNextLine As String, i As Integer, _
j As Integer, l As Integer, creation As String, accessed As String, varFile() _
As Variant
''Change the file if the file argument is filled in
If file <> "" Then
Call ChangeFile(file)
End If
''If the selected file is encrypted, decrypt it first
If strFileIndex(bytFileIndex, 2) = True Then
Call DecryptFile(strFile, strFileIndex(bytFileIndex, 3))
End If
''Load the file into a string for editing
nFileNum = FreeFile
Open strFile For Input As nFileNum
''Load the group into a string array for editing
Do While Not EOF(nFileNum)
Line Input #nFileNum, sNextLine
ReDim Preserve varFile(i, 3)
''If the line is a group header, add it to the array, otherwise set the first _
dimension to the value name, and the second to it''s value
If Left(sNextLine, 1) = "[" Then
varFile(i, 0) = Mid(sNextLine, 2, Len(sNextLine) - 2)
Else
For j = 1 To Len(sNextLine)
If Mid(sNextLine, j, 1) = "=" Then
varFile(i, 1) = Mid(sNextLine, 1, j - 1)
varFile(i, 2) = Mid(sNextLine, j + 1, Len(sNextLine) - j)
varFile(i, 3) = True ''True and false indicates whether the entry
End If ''will be rewritten. True, it will, etc.
Next
End If
i = i + 1
Loop
Close nFileNum
nFileNum = FreeFile
Open strFile For Output As nFileNum
''Blank the file line by line
Do While Not EOF(nFileNum)
Write #nFileNum, ""
Loop
''Now, the fun bit. First, we look at the groups to see if we can find the group we _
are looking for. When we do, we then look for the item name. After finding that, _
we flag that entry as boolean False.
l = i - 1
For i = 0 To l
If varFile(i, 0) = group Then
Do While varFile(i, 0) = group Or varFile(i, 0) = ""
If varFile(i, 1) = item Then
varFile(i, 3) = False
End If
i = i + 1
Loop
End If
Next
End Sub
Heres the code. As you may see, it is designed to delete a section of an INI file by loading the file into an array, section by section and then searching for the item we want to delete and flagging it to a boolean false. An unwritten section goes through the array and rewrites the boolean true values back to the file.
The problem lies in the array. For some reason, when I use the Preserve keyword, all I ever get on the loops second itteration is a code 9 error ("Subscript out of range"). It ReDims fine if I leave Preserve out, but of course the previous contents are lost...
Any ideas what might be causing it?
#Old Steve, he said to Xerox "Boys, turn your heads and cough" And when no-one was looking he ripped their interfaces off#
Three Dead Trolls in a Baggie on the truth behind MacOS 1
SketchSoft | SketchNews