vbs读取服务器上的txt文件,VBS读写txt文件常用方法
1、打開文件
使用opentextfile方法
set fs =createobject(“scripting.filesystemobject”)
set ts=fs.opentextfile(“c:\1.txt”,1,true)
注意這里需要填入文件的完整路徑,后面一個參數為訪問模式
1為forreading
2為forwriting
8為appending
第三個參數指定如果指定文件不存在,是否創建。
2、讀取文件
讀取文件的方法有三個
read(x)讀取x個字符
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
這里還要介紹幾個指針變量:
textstream對象的atendofstream屬性。當處于文件結尾的時候這個屬性返回true.我們可以用循環檢測又沒有到達文件末尾。例如:
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
還有一個屬性,atendofline,如果已經到了行末尾,這個屬性返回true.
Textstream對象還有兩個有用的屬性,column和line.
在打開一個文件后,行和列指針都被設置為1。
看一個綜合的例子吧:
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
最后說一下在文件中跳行
skip(x)? 跳過x個字符
skipline? 跳過一行
用法也很簡單 和前面一樣,就不說了。
3、寫文件
可以用forwriting和forappending方式來寫
寫有3各方法:
write(x)
writeline
writeblanklines(n) 寫入n個空行
來看一個例子:
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
注意 寫完文件以后一定要關閉!!!!!!!? 還有就是,如果要讀文件又要寫文件,讀完之后一定也要記得關閉,這樣才能以寫的方式打開。
總結
以上是生活随笔為你收集整理的vbs读取服务器上的txt文件,VBS读写txt文件常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 迪捷软件团队研发的国产替代MBSE系统建
- 下一篇: flume数据丢失与重复_Flume监听