Vbscript 脚本编程关于文件的读写
Vbscript 脚本编程关于文件的读写
使用vbscript来读写文件,十分的方便,废话少说,切入正题。
1、打开文件
使用opentextfile方法
set fs =createobject(“scripting.filesystemobject”)
set ts=fs.opentextfile(“c:\1.txt”,1,true)
注意这里需要填入文件的完整路径,后面一个参数为访问模式
1为forreading
2为forwriting
8为a
第三个参数指定如果指定文件不存在,是否创建。
2、读取文件
读取文件的方法有三个
read(x)读取x个字符
read
re
例如:
set fs =createobject(“scripting.filesystemobject”)
set ts=fs.opentextfile(“c:\1.txt”,1,true)
value=ts.read(20)
contents=ts.re
这里还要介绍几个指针变量:
textstream对象的atendofstream属性。当处于文件结尾的时候这个属性返回true.我们
set fs =createobject(“scripting.filesystemobject”)
set f=fs.getfile(“c:\1.txt”,1,false)
set ts=f.openastextstream(1,0)
do w
f.read(1)
loop
还有一个属性,atendof
Textstream对象还有两个有用的属性,column和
在打开一个文件后,行和列指针都被设置为1。
看一个综合的例子吧:
set fs =createobject(“scripting.filesystemobject”)
set f=fs.opentextfile(“c:\1.txt”,1,true)
do w
data=””
for a=1 to 5
data=data+f.read
end
next
dataset=dataset+1
wscript.
loop 最后说一下在文件中跳行
skip(x) 跳过x个字符
skip
用法也很简单 和前面一样,就不说了。
3、写文件
写有3各方法:
write(x)
write
writeblank
来看一个例子:
data=”hello, I
set fs =createobject(“scripting.filesystemobject”)
set f =fs.opentextfile(“c:\2.txt”,8)
f.write data
f.write
f.close
else
set f=fs.opentextfile(“c:\2.txt”,2, true)
f.writeblank
f.write data
f.close
end |



simeon2005
博客统计信息
热门文章
最新评论
友情链接
