我有一个文本文件,每个文件都有一些相关的参数,如下所示:
1 Thing 1
2 Attribute: X
3 Other1: foo
4 Other2: bar
5 Thing 2
6 Attribute: Y
7 Other1: foo
8 Thing 3
9 Attribute: a
10 Thing 4
11 Attribute: f
12 Thing 5
13 Attribute: m
14 EndText
其中事物编号表示以下数组中的属性位置:
Current Attribute Array: [X, Y, a, f, m]
Future Attribute Array: [X - 1, Y - 1, a - 1, f - 1, m - 1] ' (representing some mathematical operation based on conditions)
(这些数组已经存在于代码中)
假设上面的文件名为Things.txt。我希望能够逐行遍历文件,并将" attribute“从当前数组中的值更改为将来数组中的operated in属性。
请注意,这里的"OtherN“参数是为了强调文件中每个”事物“的部分在行数上不相等。
我设置了一个从lbound(currentArray)到ubound(currentArray)的for循环。
假设我们在循环迭代3中
我之前已经设置了
rowThing = 8 (the row that Thing 3's section begins)
rowNextThing = 10 (the first row that no longer applies to Thing 3)
searchText = "Attribute: a" (a concatenation of "Attribute: " and the 3rd entry in the current attribute array)
Set fso = CreateObject("Scripting.FileSystemObject")
Open filePath For Input As #1
Open tempFilePath For Append As #2
Do While Not EOF(1)
Line Input #1, textline
rowCounter = rowCounter + 1
' check the current line to see if it contains the string we're looking
' for (must be in between rowThing and rowNextThing and equal to the
' search text. If so, change whats printed in the tempFile. If not,
' just read from file and write to tempFile
If (InStr(textline, searchText) > 0 And rowCounter > rowThing And rowCounter < rowNextThing) Then
Print #2, futureAttribute ' ("Attribute: a - 1", in this case)
Else
Print #2, textline
End If
Loop
Close #1
Close #2
' delete file, rename tempFile to file (saving the newly written tempFile)
fso.DeleteFile filePath
fso.MoveFile tempFilePath, filePath
当我逐步执行代码并到达"Close #2“时,我希望看到的是一个看起来与文件完全相同的tempFile。相反,我看到了一个tempFile,它看起来很像文件的开头,但提前停止(例如,只写到第10行)。
请注意,实际的文件有888行,其中包含27种不同的内容。
有什么想法吗?
编辑:我被要求提供我的代码的更完整的记账,所以这里是
Set fso = CreateObject("Scripting.FileSystemObject")
rowThing = 0
rowNextThing = 0
' set arrays
currentValueAttributes = Array(9.9, 8.7, 4.5, 4.6, 10.1)
newValueAttributes = Array(9.7, 7.2, 5, 3.5, 7.8)
arrayThings = Array("Thing 1", "Thing 2", "Thing 3", "Thing 4", "Thing 5")
For i = LBound(arrayThings) To UBound(arrayThings) ' this should equate to for i = 0 to 4
searchTextAttribute = "Attribute: " & currentValueAttributes(i)
replaceTextAttribute = "Attribute: " & newValueAttributes(i)
searchTextThing = arrayThings(i)
If i < UBound(arrayThings) Then
searchTextNextThing = arrayThings(i + 1)
Else
searchTextNextThing = "EndText"
End If
Open filePath For Input As #1
Do While Not EOF(1)
Line Input #1, textline
rowCounter = rowCounter + 1
' check the current line to see if it contains arrayThings(i)
' if it does, set rowThing as the current line number (saves the rownum of the current Thing)
If textline Like searchTextThing Then
rowThing = rowCounter
End If
Loop
Close #1
rowCounter = 0
textline = ""
Open filePath For Input As #1
Do While Not EOF(1)
Line Input #1, textline
rowCounter = rowCounter + 1
' check the current line to see if it contains arrayThings(i + 1) or EndText
' if it does, set rowNextThing as the current line number (saves the rownum of the current NextThing)
If textline Like searchTextNextThing Then
rowNextThing = rowCounter
End If
Loop
Close #1
rowCounter = 0
textline = ""
Open filePath For Input As #1
Open tempFilePath For Append As #2
Do While Not EOF(1)
Line Input #1, textline
rowCounter = rowCounter + 1
' check the current line to see if it contains the string we're looking
' for (must be in between rowThing and rowNextThing and equal to the
' search text. If so, change whats printed in the tempFile. If not,
' just read from file and write to tempFile
If (InStr(textline, searchTextAttribute) > 0 And rowCounter > rowThing And rowCounter < rowNextThing) Then
Print #2, replaceTextAttribute
Else
Print #2, textline
End If
Loop
Close #1
Close #2
' delete file, rename tempFile to file (saving the newly written tempFile)
fso.DeleteFile filePath
fso.MoveFile tempFilePath, filePath
Next i
转载请注明出处:http://www.qxdxgs.com/article/20230526/2021563.html