【问题标题】:How do I Replace a String in a Line of a Text File Using FileSystemObject in VBA?如何在 VBA 中使用 FileSystemObject 替换文本文件行中的字符串?
【发布时间】:2013-10-22 17:05:57
【问题描述】:

我正在尝试使用 FileSystemObject 方法在文本文件中查找特定行,并在该行中替换特定字符串。我对此比较陌生,因为我当前的代码已经用excel打开了文本文件并替换了我需要替换的内容,然后保存并关闭它。这种方式不再是一种选择,因为用 excel 打开文本文件需要很长时间并且会占用文件。

这是我到目前为止所取得的进展。

-

Sub FindLines()

Const ForReading = 1

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objFSO = FSO.OpenTextFile("C:\Users\Carella Home\Desktop\boomboom.txt", ForReading,     False)

Do Until objFSO.AtEndOfStream = True

    go = objFSO.ReadLine

    If InStr(1, go, "ant", vbTextCompare) > 0 Then

        bo = Replace(go, "t", "wow")

    End If

Loop

objFSO.Close

Set objFSO = FSO.OpenTextFile("C:\Users\Carella Home\Desktop\boomboom.txt", 2)

End Sub

-

我能做的最好的就是打开要写入的文件,但我不知道如何找到该行并将其替换为我需要替换它的行。

如果您愿意在正确的方向上帮助/指导我,如果您需要更多信息,请告诉我。我进行了很多搜索,并看到人们提出了其他方法。我需要学习如何以这种方式编辑线条。有人可以帮帮我吗?

提前致谢!

-安东尼 C.

【问题讨论】:

  • 出于好奇,为什么要使用 excel 而不是 word 或其他文本阅读器?
  • 我使用的是 excel,因为这是最初教给我的。 VBA 代码位于许多用户使用的文件中,因此在有 10 个这些应用程序通过打开/关闭 excel 读取/写入同一个文件之后太多并导致太多错误。我需要比打开和关闭文件更快的东西。

标签: excel file vba text filesystemobject


【解决方案1】:

不确定这是否是最有效的方法,但一个想法是使用FileSystemObject 的CreateTextFile 方法来创建另一个您可以写入的文件。

我已经在一个小文件上对此进行了测试,并且似乎可以按预期工作。

在接受答案后修改以避免.ReadLine 和.WriteLine 循环

Sub FindLines()
'Declare ALL of your variables :)
Const ForReading = 1    '
Const fileToRead As String = "C:\Users\david_zemens\Desktop\test.txt"  ' the path of the file to read
Const fileToWrite As String = "C:\Users\david_zemens\Desktop\test_NEW.txt"  ' the path of a new file
Dim FSO As Object
Dim readFile As Object  'the file you will READ
Dim writeFile As Object 'the file you will CREATE
Dim repLine As Variant   'the array of lines you will WRITE
Dim ln As Variant
Dim l As Long

Set FSO = CreateObject("Scripting.FileSystemObject")
Set readFile = FSO.OpenTextFile(fileToRead, ForReading, False)
Set writeFile = FSO.CreateTextFile(fileToWrite, True, False)

'# Read entire file into an array & close it
repLine = Split(readFile.ReadAll, vbNewLine)
readFile.Close

'# iterate the array and do the replacement line by line
For Each ln In repLine
    ln = IIf(InStr(1, ln, "ant", vbTextCompare) > 0, Replace(ln, "t", "wow"), ln)
    repLine(l) = ln
    l = l + 1
Next

'# Write to the array items to the file
writeFile.Write Join(repLine, vbNewLine)
writeFile.Close

'# clean up
Set readFile = Nothing
Set writeFile = Nothing
Set FSO = Nothing

End Sub

然后,根据您是否要摆脱“原始”文件,您可以执行以下操作:

'# clean up
Set readFile = Nothing
Set writeFile = Nothing
'# Get rid of the "old" file and replace/rename it with only the new one
Kill fileToRead
Name fileToWrite As fileToRead
End Sub

【讨论】:

  • 非常感谢。我以前见过这种用法,但从未向我解释清楚。我一定会试试这个并发布我的结果。
  • 我刚刚对此进行了测试,它运行良好。非常感谢。
  • 我在工作中运行此代码,今天(我不确定是什么)我丢失了原始文件中的大部分数据。有没有办法在不删除和替换原始文件的情况下使用 fso 来做到这一点?
  • 我认为不应该这样做的原始答案,因为您使用ForReading 打开readFile。如果你做了底部的可选部分,Kill fileToRead: Name fileToWrite As fileToRead,则删除原始文件并用新文件替换它。
  • 我明白了。有没有办法读取文件,找到需要编辑/替换的内容,然后写入/编辑相同的文件?我有几个不同的应用程序以及一个使用此文本文件的 oracle 数据库,所以每隔几天我就会在处理文件切换时遇到一些错误。
【解决方案2】:

与FileSystemObject相比,这是一种更快、更短的方法

Sub Sample()
    Dim MyData As String, strData() As String

    '~~> Read the file in one go!
    Open "C:\Users\Carella Home\Desktop\boomboom.txt" For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Close #1
    strData() = Split(MyData, vbCrLf)
End Sub

您的所有文本文件数据现在都在数组strData 中,只需循环遍历数组并找到您要替换的文本并将其写回 SAME 文件。

【讨论】:

  • 我以前见过这种方法,并建议通过filesystemobject引用文件,但从未解释过。我会查一下语法,看看我能学到多少。由于我已经通过文件系统对象附加了我的文件,我认为这也是使用它来编辑文件的更好方法。我会仔细看看的。感谢您的回复。
  • 我不熟悉这个。尝试这种方法时,我没有什么可参考的。但是,我很好奇为什么这会更有效。这个叫什么?您能否为我指明正确的方向,以了解如何使用您在回复中输入的所有内容?谢谢!
  • 原因很简单。此方法更快,因为您没有循环遍历文本文件的每一行。您正在做的是一次性加载数组中的完整文本文件(到内存中),然后对该数组执行操作。
  • +1 这也是我能想到的另一种方法。我修改了我的答案以避免.ReadLine 和.WriteLine 循环。
  • 该方法没有name。我只是以二进制模式读取文件,然后使用vbcrlf 作为分隔符将其存储在数组中。
猜你喜欢
  • 2014-11-07
  • 2021-02-08
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 2015-11-23
相关资源
最近更新 更多