如何用VBS逐行读取txt文件的内容

za521mao 2006-08-23 04:07:16
在txt文件中有如下格式的数据:
10899
10900
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20111
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20201
20202
20210
如何用程序逐行把这些数据读取出来??
...全文
3314 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
feillex 2006-09-16
  • 打赏
  • 举报
回复
提供两种方法参考
1、一次性读出文件内所有内容,然后逐行处理
option explicit
dim fso,fread,str,strarry,linestr
set fso=createobject("scripting.filesystemobject")
set fread=opentextfile("test.txt",1)
str=fread.readall
fread.close
if str="" then
wscript.echo "file have not any content"
wscript.quit
end if
strarry=split(str,vbcrlf)
for each linestr in strarry
wscript.echo linestr '这里是用echo显示每一行的内容,你可以根据自己的需要做别的处理
next
set fso=nothing

2、每次读出一行内容处理
option explicit
dim fso,fread,strline
set fso=createobject("scripting.filesystemobject")
set fread=opentextfile("test.txt",1)
do until fread.atendofstream
strline=fread.readline
wscript.echo strline '这里也仅仅是显示一行内容而已
loop
fread.close
set fso=nothing
za521mao 2006-08-24
  • 打赏
  • 举报
回复
米人知道吗?
三仙半 2006-08-24
  • 打赏
  • 举报
回复
用FSO的方法,下面是VBS帮助里给的一个读、写文件的示例
Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
' 写一行。
Response.Write "Writing file <br>"
f1.WriteLine "Hello World"
f1.WriteBlankLines(1)
f1.Close
' 读取文件的内容。
Response.Write "Reading file <br>"
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
s = ts.ReadLine
Response.Write "File contents = '" & s & "'"
ts.Close
End Sub

参考吧!

4,008

社区成员

发帖
与我相关
我的任务
社区描述
它是一种微软环境下的轻量级的解释型语言,它使用COM组件、WMI、WSH、ADSI访问系统中的元素,对系统进行管理。
社区管理员
  • vbScript社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧