Board logo

标题: VBS 里有没有类似于 CMD 的 Find? [打印本页]

作者: huzixuan     时间: 2007-7-6 10:46    标题: VBS 里有没有类似于 CMD 的 Find?

有一文本内容
123
adb
322
31a
121
hfh
我想找到 包含 "3" 的行
用批处理这样做
@echo off
find "3" test.txt
pause
用 VBS 怎么做? 一个一个读吗?
作者: lxmxn     时间: 2007-7-6 10:47
用VBS的正则表达式,搜索一下。
作者: lxmxn     时间: 2007-7-6 11:14
VBSCRIPT.chm 上面的一个例子:
Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches   ' 建立变量。
   Set regEx = New RegExp   ' 建立正则表达式。
   regEx.Pattern = patrn   ' 设置模式。
   regEx.IgnoreCase = True   ' 设置是否区分字符大小写。
   regEx.Global = True   ' 设置全局可用性。
   Set Matches = regEx.Execute(strng)   ' 执行搜索。
   For Each Match in Matches   ' 遍历匹配集合。
      RetStr = RetStr & "Match found at position "
      RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
      RetStr = RetStr & Match.Value & "'." & vbCRLF
   Next
   RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

作者: huzixuan     时间: 2007-7-6 12:36
上面那个是什么意思,是从哪里搜索比对的,可以从文本里搜索吗
作者: jmz573515     时间: 2007-7-6 16:01

set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile("test.txt")
    s=split(file.readall,vbcrlf):mys=filter(s,"3")
file.close
msgbox join(mys,vbcrlf)
[ Last edited by jmz573515 on 2007-7-6 at 06:10 PM ]
作者: sonicandy     时间: 2007-7-6 22:42
我来个比较通俗一点的
option explicit '严格语法
dim found,fso,file,str
set fso=createobject("scripting.filesystemobject") '创建文件系统对象
set file=fso.opentextfile("test.txt")                    '打开文件,并得到文件对象
found = "" '初始化缓冲区
while not file.atendofstream '只要文件没有结束就循环
    str = file.readline '读取一行内容
    if instr(str,"3")>0 then '判断是否包含3
        found = found & str & vbcrlf '如果包含就加入到缓冲区
    end if
wend
file.close '关闭文件
msgbox found '显示结果
set fso = nothing'释放对象
set file = nothing '(不知道这句该加么?)
[ Last edited by sonicandy on 2007-7-6 at 10:49 PM ]
作者: zhoushijay     时间: 2007-7-7 13:27
set fso=createobject("scripting.filesystemobject")
set op=fso.opentextfile("test.txt")
i_num=0
do while op.atendofstream=false
        line=op.readline
        if instr(1,line,"3")<>0 then
                ret=ret&line&chr(10)
        end if
loop
msgbox (ret)
作者: zerocq     时间: 2007-7-7 17:41
instr函数算是吧??