Sha*_*ank -2 vbscript excel vba excel-vba
嗨,我是VB的新手,只是如果有人可以在我目前的情况下帮助我
我有一个名为shar.txt的文本文件,
它有6行.
我是一名新生,
我正在学习VB
请帮助我的朋友朋友
们一生都很重要
谢谢你的支持
永远感激你
我想要一个脚本,它读取这个文本文件,并查找字符串,如"朋友","支持",并在同一位置打印包含这些字符串的行在另一个文本文件中说"sha.txt"
我试过这一点但是我中途迷失了方向.
请有人帮助我.
谢谢
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
Run Code Online (Sandbox Code Playgroud)
这个网站形成了一个非常糟糕的问题.花些时间阅读规则对你有好处.
无论如何,这是来自我.
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
Run Code Online (Sandbox Code Playgroud)
编辑:关于使用数组的问题...
' 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
Run Code Online (Sandbox Code Playgroud)
干杯!
| 归档时间: |
|
| 查看次数: |
26317 次 |
| 最近记录: |