当前位置: 代码迷 >> vbScript >> vbs文件搜索及过虑有关问题
  详细解决方案

vbs文件搜索及过虑有关问题

热度:4329   发布时间:2013-02-26 00:00:00.0
vbs文件搜索及过虑问题
如我有一个文件1.txt内容如下:
xxxxxxxx1xxxxxxxx
a1
b1
c1
xxxxxxxx2xxxxxxxxxx
a2
b2
c3
xxxxxxxx3xxxxxxxx
a3
b3
c3

我想写一个vbs脚本读取1.txt,搜索x2关键字,并同时把从x2当作开始行的4行内容输出到2.txt。我不知道怎么做?大家有什么好的方法吗?
如果脚本运行正确,应当2.txt是显示:
xxxxxxxx2xxxxxxxxxx
a2
b2
c3

------解决方案--------------------------------------------------------
VBScript code
    sFile = "a.txt"Set oFSO = CreateObject("Scripting.FileSystemObject")Set oFile = oFSO.OpenTextFile(sFile)    iCount = 0'Do Until oFile.AtEndOfLine'    str = oFile.ReadLine'    Select Case iCount'        Case 0'            If InStr(str,"2x") Then'                iCount = 1'                WScript.Echo str'            End If'        Case 1,2,3'            WScript.Echo str'            iCount = iCount + 1'        Case Else'    End Select'Loop    For i = 0 To 3        str = oFile.ReadLine        If InStr(str,"2x") Then            WScript.Echo str        ElseIf i > 0 Then            WScript.Echo str        Else            i = i - 1        End If        If oFile.AtEndOfLine Then Exit For    Next    oFile.Close    Set oFile = Nothing    Set oFSO = Nothing
------解决方案--------------------------------------------------------
第四行插入:
Set oFileWrite = oFSO.OpenTextFile("b.txt",2,True)
然后下面的 WScript.Echo 替换成 OFileWrite.WriteLine
最后加上: 
oFileWrite.Close
Set OfileWrite = Nothing
  相关解决方案