vba 在 vb 中搜索文本文件中的字符串并打印行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17275040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:57:13  来源:igfitidea点击:

search for string in text file in vb and print lines

excel-vbavbscriptvbaexcel

提问by Shashank

Hi I am new to VB and just though if someone could help me in my current scenario

嗨,我是 VB 的新手,尽管有人可以在我目前的情况下帮助我

I have a text file named shar.txt
It has 6 lines in it.

我有一个名为 shar.txt 的文本文件,
其中有 6 行。

I am a new student
I am learning VB
Please help me friends
Friends always matter in our life
Thank You for your support
Always grateful to you

我是新学生
我正在学习VB
请帮助我的朋友
朋友在我们的生活中很重要
谢谢您的支持
永远感谢您

I want a script which reads this text file and look for the string such as "Friends", "support" and print the lines containing those strings in another text file at the same location say "sha.txt"

我想要一个脚本来读取此文本文件并查找诸如“Friends”、“support”之类的字符串,并在同一位置的另一个文本文件中打印包含这些字符串的行,例如“sha.txt”

I tried till this point but lost my way i mid.

我一直尝试到这一点,但我在中间迷路了。

Please someone help me.
thanks

请有人帮助我。
谢谢

Sub ReadToTextFile()
Dim strPattern1 As String    
Dim strPattern2 As String    
H1 As String    
H2 As String    
strPattern1 = "friends"    
strPattern2 = "support"    


Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\sonu\Desktop\auto\shar.txt", 1, True)    
Do Until
 objFileToRead.AtEndOfStream        
    strLine = objFileToRead.ReadLine        
    ElseIf 
InStr(strLine, strPattern1) > 0    
 Then        
        Wscript.Echo strLine    
                H1 = strLine    
                ElseIf 
InStr(strLine, strPattern2) > 0        
 Then        
                Wscript.Echo strLine    
                H2 = strLine    
           End If    

    End If    
Loop    

Wscript.Echo H2    

Set objFileToRead = Nothing    

End Sub    

回答by Panayot Karabakalov

A very bad formed question for this site. It's good for you to spend some time to read the rules.

这个网站的一个非常糟糕的问题。花一些时间阅读规则对你有好处。

Anyway, this is from me.

无论如何,这是我的。

Const ForReading = 1, ForWriting = 2
Dim FSO, FileIn, FileOut, strTmp

Set FSO     = CreateObject("Scripting.FileSystemObject")
Set FileIn  = FSO.OpenTextFile("shar.txt", ForReading)
Set FileOut = FSO.OpenTextFile("sha.txt", ForWriting, True)

Do Until FileIn.AtEndOfStream
    strTmp = FileIn.ReadLine
    If Len(strTmp) > 0 Then
        If InStr(1, strTmp, "Friends", vbTextCompare) > 0 _
        Or InStr(1, strTmp, "support", vbTextCompare) > 0 Then
            FileOut.WriteLine strTmp
        End If
    End If
Loop

FileIn.Close
FileOut.Close

EDIT: About your question for using arrays...

编辑:关于您使用数组的问题...

' an example array
arWords = Array("friends", "support", "xyz")
' modified Do..Loop
Do Until FileIn.AtEndOfStream
    strTmp = FileIn.ReadLine
    If Len(strTmp) > 0 Then
        For i = 0 To UBound(arWords)
            If InStr(1, strTmp, arWords(i), vbTextCompare) > 0 Then
                FileOut.WriteLine strTmp
                Exit For
            End If
        Next
    End If
Loop

Cheers!

干杯!