Board logo

标题: [求助]怎样用vbs代码实现查找和写出字符串? [打印本页]

作者: s11ss     时间: 2007-12-12 19:51    标题: [求助]怎样用vbs代码实现查找和写出字符串?
怎样用vbs代码实现查找某个文本文件中的某个字符串并写出到另一个文件?(输出包含该字符串的行)

Last edited by s11ss on 2007-12-14 at 04:24 PM ]

作者: vkill     时间: 2007-12-12 20:47
open file
str = read file
colse flie
str=~

作者: s11ss     时间: 2007-12-12 21:10
vkill兄知道不知道用cscript执行vbs时是否有类似cls的清屏命令?

作者: vkill     时间: 2007-12-13 20:30
Originally posted by s11ss at 2007-12-12 21:10:
vkill兄知道不知道用cscript执行vbs时是否有类似cls的清屏命令?

未知,好象除了cmd都没有清屏的功能吧

作者: zh159     时间: 2007-12-13 20:48
Originally posted by vkill at 2007-12-13 20:30:

未知,好象除了cmd都没有清屏的功能吧

hta可以有

作者: jmz573515     时间: 2007-12-13 21:37
hta怎么清民屏啊?

作者: lotus516     时间: 2007-12-14 00:29
能不能把代码说详细些啊?

作者: zh159     时间: 2007-12-14 02:09
并不是真正的清屏,而是替换为空或者隐藏
hta本身运行方式就和批处理不同

<html>
<title>cls</title>
<HTA:APPLICATION
ID="MyhyliApp"
APPLICATIONNAME="cls"
BORDER="thin"
BORDERSTYLE="normal"
VERSION="1.0"
SCROLL="no"
INNERBORDER="yes"
CONTEXTMENU="yes"
CAPTION="yes"
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
WINDOWSTATE="normal"
NAVIGABLE="yes"
/>

<script language="VBScript">

windowswidth = 640
windowsheight = 480
window.resizeTo windowswidth, windowsheight
ileft=(window.screen.width-windowswidth)/2
itop=(window.screen.height-windowsheight)/2
window.moveTo ileft,itop

</script>

<body bgcolor=#e0e0e0>

<table align="center">
<tr>
<td align="center">清除后用F5刷新</td>
</tr>
<tr>
<td id="Menu" align="center"></td>
</tr>
</table>


<table width="100%" style="position:absolute; bottom:20px;"><td>
<table align="center" style="font:bold 15px 宋体;">
<tr align="center">
<td><button onclick="CLS1" style="width:100;">清除内容</button></td>
</tr>
<tr align="center">
<td><button onclick="CLS2" style="width:100;">隐藏内容</button></td>
</tr>
</table>
</td></table>

</body>

<script language="VBScript">

Menu.innerHTML = "测试<br>测试<br>测试<br>测试<br>测试<br>"

Sub CLS1
Menu.innerHTML = ""
End Sub

Sub CLS2
Menu.style.display = "none"
End Sub

</script>

<html>

作者: fastslz     时间: 2007-12-14 15:00
指定1个字符
file=Wscript.ScriptFullName
'file=WScript.Arguments(0)
Dim str
set str = CreateObject("Adodb.Stream")
str.Type = 1
str.Mode = 3
str.Open
str.Loadfromfile file
Bin=str.read(6)
Usage = MidB(Bin,6,1)
str.Close
set str = Nothing
WScript.echo "本脚本第6字节字符为“",Usage,"”"

作者: zh159     时间: 2007-12-14 15:39
问题是你要输出包含字符的那一行,还是有那个字符就输出那个字符?

作者: s11ss     时间: 2007-12-14 16:23
输出包含字符的那一行

作者: zh159     时间: 2007-12-14 16:42
Set fso = CreateObject("Scripting.FileSystemObject")

source = "a.txt"
strFind = "b"

Set f = fso.OpenTextFile(source,1)
While not f.AtEndOfStream
strRead = f.ReadLine
If instr(strRead,strFind)>0 Then
msgbox strRead
End If
Wend
f.Close

作者: fastslz     时间: 2007-12-14 17:03
文本中第几个字节,换行+2

作者: terse     时间: 2007-12-14 20:16
翻到个老贴发上来

set fso=createobject("scripting.filesystemobject")
If fso.FileExists("text.txt")=0 then '判断是否存在text.txt文件
fso.createtextfile("text.txt")
end if
set file=fso.opentextfile("text.txt",8) '追加形式打开文件
set opfile=fso.opentextfile("a.txt") '打开文件
do while opfile.atendofstream=false
line=opfile.readline '读取一行内容
if instr(line,"查找字符")>0 then '判断是否包含查找字符
str=str&line&vbcrlf '加入缓冲区
end if
loop
file.writeline (str) '搜索到查找字符行写入text.txt文件

作者: lotus516     时间: 2007-12-16 10:48
Originally posted by zh159 at 2007-12-14 16:42:
Set fso = CreateObject("Scripting.FileSystemObject")

source = "a.txt"
strFind = "b"

Set f = fso.OpenTextFile(source,1)
While not f.AtEndOfStream
strRe ...

如果有多个字符呢!!我有一个文本list.txt,里面有abc,will,www等等(均为一行一个单词),可不可以一起找出来呢?而不要运行VBS都要去重新给strFind赋值

作者: zh159     时间: 2007-12-16 14:44
Set fso = CreateObject("Scripting.FileSystemObject")

source = "a.txt"
strFind = "a,b,c,d,e,f"
strFind = Split(strFind,",")
Num = Ubound(strFind)

Set f = fso.OpenTextFile(source,1)
While not f.AtEndOfStream
strRead = f.ReadLine
For i = 0 to Num
If instr(strRead,strFind(i))>0 Then
msgbox strRead
End If
Next
Wend
f.Close

这里查找的字符区分大小写(执行的是二进制比较,具体查看instr用法)

Last edited by zh159 on 2007-12-16 at 02:46 PM ]

作者: lotus516     时间: 2007-12-17 08:12
Originally posted by zh159 at 2007-12-16 14:44:
Set fso = CreateObject("Scripting.FileSystemObject")

source = "a.txt"
strFind = "a,b,c,d,e,f"
strFind = Split(strFind,",")
Num = Ubound(strFind)
...

zh159理解错我的意思了,我是说strFind的值从某一个文本中取得,不是后面跟一串,总不可能我要找50个,strFind后面打上50个变量吧!!

作者: zh159     时间: 2007-12-17 10:06
Originally posted by lotus516 at 2007-12-17 08:12:

zh159理解错我的意思了,我是说strFind的值从某一个文本中取得,不是后面跟一串,总不可能我要找50个,strFind后面打上50个变量吧!!

Set fso = CreateObject("Scripting.FileSystemObject")

source = "a.txt"
target = "b.txt"

Set f = fso.OpenTextFile(source,1)
strFind = f.ReadAll
f.Close

Set f = fso.OpenTextFile(target,1)
While not f.AtEndOfStream
strRead = f.ReadLine
If instr(1,strFind,strRead,1)=0 Then
str = str & strRead & vbCrLf
End If
Wend
f.Close
msgbox str