vbs读取服务器上的txt文件,VBS读写txt文件常用方法
1、打開(kāi)文件
使用opentextfile方法
set fs =createobject(“scripting.filesystemobject”)
set ts=fs.opentextfile(“c:\1.txt”,1,true)
注意這里需要填入文件的完整路徑,后面一個(gè)參數(shù)為訪(fǎng)問(wèn)模式
1為forreading
2為forwriting
8為appending
第三個(gè)參數(shù)指定如果指定文件不存在,是否創(chuàng)建。
2、讀取文件
讀取文件的方法有三個(gè)
read(x)讀取x個(gè)字符
readline讀取一行
readall全部讀取
例如:
set fs =createobject(“scripting.filesystemobject”)
set ts=fs.opentextfile(“c:\1.txt”,1,true)
value=ts.read(20)
line=ts.readline
contents=ts.readall
這里還要介紹幾個(gè)指針變量:
textstream對(duì)象的atendofstream屬性。當(dāng)處于文件結(jié)尾的時(shí)候這個(gè)屬性返回true.我們可以用循環(huán)檢測(cè)又沒(méi)有到達(dá)文件末尾。例如:
set fs =createobject(“scripting.filesystemobject”)
set f=fs.getfile(“c:\1.txt”,1,false)
set ts=f.openastextstream(1,0)
do while ts.atendofstream<>true
f.read(1)
loop
還有一個(gè)屬性,atendofline,如果已經(jīng)到了行末尾,這個(gè)屬性返回true.
Textstream對(duì)象還有兩個(gè)有用的屬性,column和line.
在打開(kāi)一個(gè)文件后,行和列指針都被設(shè)置為1。
看一個(gè)綜合的例子吧:
set fs =createobject(“scripting.filesystemobject”)
set f=fs.opentextfile(“c:\1.txt”,1,true)
do while f.atendofstream<>true
data=””
for a=1 to 5
if f.atendofstream<>true then
data=data+f.readline
end if
next
dataset=dataset+1
wscript.echo “data set” &dataset & ”:” & data
loop
最后說(shuō)一下在文件中跳行
skip(x)? 跳過(guò)x個(gè)字符
skipline? 跳過(guò)一行
用法也很簡(jiǎn)單 和前面一樣,就不說(shuō)了。
3、寫(xiě)文件
可以用forwriting和forappending方式來(lái)寫(xiě)
寫(xiě)有3各方法:
write(x)
writeline
writeblanklines(n) 寫(xiě)入n個(gè)空行
來(lái)看一個(gè)例子:
data=”hello, I like script programing”
set fs =createobject(“scripting.filesystemobject”)
if (fs.fileexists(“c:\2.txt”)) then
set f =fs.opentextfile(“c:\2.txt”,8)
f.write data
f.writeline data
f.close
else
set f=fs.opentextfile(“c:\2.txt”,2, true)
f.writeblanklines 2
f.write data
f.close
end if
注意 寫(xiě)完文件以后一定要關(guān)閉!!!!!!!? 還有就是,如果要讀文件又要寫(xiě)文件,讀完之后一定也要記得關(guān)閉,這樣才能以寫(xiě)的方式打開(kāi)。
總結(jié)
以上是生活随笔為你收集整理的vbs读取服务器上的txt文件,VBS读写txt文件常用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 迪捷软件团队研发的国产替代MBSE系统建
- 下一篇: flume数据丢失与重复_Flume监听