Vba 如何使用;附加“;使用Visual Basic在文本文件中的特定位置上执行命令?
文本文件: 测试1 测试2 测试3 测试4 我想在“test2”之后写一些东西。 我第一次尝试打开文件,我读取了test2的位置。之后,我打开了文本文件的附加格式,但我不知道如何写在特定的位置 我必须在Visual Basic中使用next命令或其他命令:Vba 如何使用;附加“;使用Visual Basic在文本文件中的特定位置上执行命令?,vba,text-files,file-manipulation,Vba,Text Files,File Manipulation,文本文件: 测试1 测试2 测试3 测试4 我想在“test2”之后写一些东西。 我第一次尝试打开文件,我读取了test2的位置。之后,我打开了文本文件的附加格式,但我不知道如何写在特定的位置 我必须在Visual Basic中使用next命令或其他命令: 对于从文本文件读取:打开Test_Filename作为输入# 用于在文本文件中追加:打开Test_Filename以追加为#3 任何帮助都会很好 编辑: LineNum=1 LineRead=0 Test\u Filename=“pat
- 对于从文本文件读取:打开Test_Filename作为输入#
- 用于在文本文件中追加:打开Test_Filename以追加为#3
编辑:
LineNum=1
LineRead=0
Test\u Filename=“path/Test.txt”
如果Len(Dir$(Test_Filename))=0,则
MsgBox(“找不到文件”)
如果结束
打开Test#u Filename以作为#3输入
不执行时执行EOF(3)
LineNum=1
strData1=“”
LineNext=0
对于LineNum=1到2
如果EOF(3),则
退出
如果结束
线路输入#3,strLine
错误转到0时重置错误处理
LineRead=LineRead+1
如果左(strLine,4)=“test1”,则
strData1=“test1”
LineNum=1
如果结束
如果(strData1=“test1”)和(左(strLine,2)=“test2”)和(LineNum=2),则
strData2=strData2&strLine+vbCrLf
strData1=“”
其他的
strData2=“”
如果结束
下一行数
环
关闭#3
打开附加为#3的测试文件名
如果(InStr(strData2,“test2”)为0,则
打印#3,“某物”
其他的
打印#3,“错误”
如果结束
关闭#3
在这段代码中,我想在“test2”和其他条件“Something”之后编写。我必须打开文件两次,因为当我用“For Input”打开文件时,我无法写入文件
但是在这种情况下,我在文件的末尾写了“某物”,我必须在“test2”之后写到一个特定的位置。您可以使用:
Print #3, "This is my text to write to file."
例如。如果你需要这方面的更多信息,请发布你目前掌握的代码
编辑: 试试这个:
Sub testWriteFile()
Const Test_Filename = "c:\testfile.txt"
Const findText = "test2" ' what to find in file
Const insertText = "!!!SOMETHING!!!" ' what to put in line after [findText]
Dim LinesRead As Integer, LinesInserted As Integer, outputText As String
'If file doesn't exist then exit sub
If Dir(Test_Filename) = "" Then
MsgBox ("Cannot find the file")
Exit Sub
End If
Open Test_Filename For Input As #3
Do While Not EOF(3)
'read line from input file
Line Input #3, strLine
LinesRead = LinesRead + 1
'write line to output string
outputText = outputText & strLine & vbCrLf
'if line says
If LCase(Left(strLine, Len(findText))) = LCase(findText) Then
outputText = outputText & insertText & vbCrLf ' "insert" this line at the current position of output string
LinesInserted = LinesInserted + 1
End If
Loop
'close output file
Close #3
'replace output file with output string
Open Test_Filename For Output As #3
Print #3, outputText
Close #3
End Sub
不能在文件的中间写<代码>追加< /代码>(即写入文件的末尾)信息。如果可能的话,这将被称为
Insert
,但这是不可能的。您需要读取原始文件,直到您希望在将内容写入新文件时插入某些内容为止。这样做时,请将新信息写入新文件,然后继续读取旧文件并写入新文件。这是我想要的,但当我在代码中翻译时无法工作。要选择多个选项,请选择“打开-关闭”文件。第一次:打开-关闭文件以进行输入(读取),第二次打开-关闭文件以进行追加,最后打开-关闭以进行输入(读取)。我遇到的问题是:当文件关闭时,该行没有保存,当我重新打开进行追加时,该单词被放在文件的末尾。“追加”表示“添加到末尾”-它不是指“插入中间的某个位置”。因此,很自然,如果您以Append
模式打开文件,它会在最后添加数据。我不希望使用“For Output”,因为覆盖所有文件没有意义。当然,如果我没有找到另一个变体,我会用它来代替你的想法。谢谢有没有理由不覆盖整个文件?没有办法“插入”到文本文件中(从技术上讲,*整个文件在任何时候编辑后都会被Windows文件系统重新写入),我将在一个非常大的文件中使用此功能,并且我不希望覆盖。除了“伪插入”行之外,对文件的唯一更改是文件的“创建/修改/访问日期”,但如果需要,也可以在新文件中复制这些内容。唯一知道不是原始文件的人是你。。。而我。。。(其他人嘘,不要说!)@Pop-Adrian文件的大小与此无关,因为就像任何文本编辑器一样,在编辑文件时,整个文件都会被删除并重新写入。(Windows只是让我们认为它是同一个文件。)无论文本文件大小如何,速度都不会受到影响。如果你找到另一种方法,一定要让我们其他人知道!另外,你在这里还是很新的,正如你所知道的,如果你得到了一个“可接受的”答案,并且对你有效,一定要点击它的“复选标记”。这给了答案的海报10个代表点,再加上2个代表点,让你接受答案。祝你好运
Sub testWriteFile()
Const Test_Filename = "c:\testfile.txt"
Const findText = "test2" ' what to find in file
Const insertText = "!!!SOMETHING!!!" ' what to put in line after [findText]
Dim LinesRead As Integer, LinesInserted As Integer, outputText As String
'If file doesn't exist then exit sub
If Dir(Test_Filename) = "" Then
MsgBox ("Cannot find the file")
Exit Sub
End If
Open Test_Filename For Input As #3
Do While Not EOF(3)
'read line from input file
Line Input #3, strLine
LinesRead = LinesRead + 1
'write line to output string
outputText = outputText & strLine & vbCrLf
'if line says
If LCase(Left(strLine, Len(findText))) = LCase(findText) Then
outputText = outputText & insertText & vbCrLf ' "insert" this line at the current position of output string
LinesInserted = LinesInserted + 1
End If
Loop
'close output file
Close #3
'replace output file with output string
Open Test_Filename For Output As #3
Print #3, outputText
Close #3
End Sub