如何使用VBA中的FileSystemObject替换文本文件行中的字符串? [英] How do I Replace a String in a Line of a Text File Using FileSystemObject in VBA?

查看:225
本文介绍了如何使用VBA中的FileSystemObject替换文本文件行中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我到目前为止已经有多远了。 / p>

-

  Sub FindLines()

Const ForReading = 1

设置FSO = CreateObject(Scripting.FileSystemObject)

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

Do Until objFSO.AtEndOfStream = True

go = objFSO.ReadLine

如果InStr(1,go,ant,vbTextCompare)> 0然后

bo =替换(go,t,wow)

如果

循环

objFSO.Close

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

结束Sub

-



做是打开文件写入,但我不知道如何找到该行,并用我需要替换它的行替换它。



请让我知道,如果您愿意帮助/指导我正确的方向,您需要更多的信息。我已经搜查了很多,并且看到人们提出了其他的方法。我需要学习如何以这种方式编辑行。有人可以帮我吗?



提前感谢!



-Anthony C。

解决方案

不知道这是否是最有效的方法,但一个想法是使用 FileSystemObject 的/en-us/library/aa265018(v=vs.60).aspxrel =noreferrer> CreateTextFile 方法,以创建另一个文件可以写入。



我已经在一个小文件中测试了这个文件,似乎按预期工作。



在接受回应后修改,以避免 .ReadLine .WriteLine 循环

  Sub FindLines()
'声明所有变量:)
Const ForReading = 1'
Const fileToRead As String =C:\Users\david_zemens\Desktop\test.txt要读取的文件的路径
Const fileToWrite As String =C:\Users\david_zemens \Desktop\test_NEW.txt'新文件的路径
Dim FSO As Object
Dim readFile As Object'将要读取的文件
Dim writeFile As Object'该文件将CREATE
Dim repLine As Variant'您将要写入的行数组
Dim ln As Variant
Dim l As Long

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

'#将整个文件读入数组&关闭它
repLine = Split(readFile.ReadAll,vbNewLine)
readFile.Close

'#迭代数组,逐行执行替换
对于每个ln在repLine
ln = IIf(InStr(1,ln,ant,vbTextCompare)> 0,Replace(ln,t,wow),ln)
repLine(l)=将$数组项写入文件
writeFile.Write Join(repLine,vbNewLine)
writeFile 。关闭

'#清理
设置readFile =没有
设置writeFile =没有
设置FSO =没有

End Sub

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

 '#清理
设置readFile = Nothing
设置writeFile = Nothing
'#获取删除旧文件并替换/重命名它只有新的一个
杀死fileToRead
名称fileToWrite As fileToRead
End Sub


I am trying to use FileSystemObject methods to find a specific line in a text file, and within that line replace a specific string. I am relatively new to this, as my current code has excel open the text file and replace what I need it to replace then save and close it. This way is no longer an option, as having excel open the text file takes too long and holds up the file.

This is how far I have gotten so far.

-

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

-

The best I can do is open the file up to write, but I have no idea how to find the line and replace it with the line that I need to replace it with.

Please let me know if, in the event that you are willing to help/guide me in the correct direction, you need more information. I have searched a lot and have seen people suggest other ways of doing this. I need to learn how to edit lines this way. Can someone please help me?

Thanks in advance!

-Anthony C.

解决方案

Not sure if this is the most efficient method but one idea would be to use the CreateTextFile method of the FileSystemObject, to create another file you can write to.

I've tested this on a small file and appears to be working as expected.

Modified after answer accepted to avoid .ReadLine and .WriteLine loops

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

Then, depending on whether you want to get rid of the "original" file you could do something like:

'# 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

这篇关于如何使用VBA中的FileSystemObject替换文本文件行中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆