VBS操作文件之一-----读写txt

上一篇 / 下一篇  2010-12-01 12:45:23 / 个人分类:VBScript

51Testing软件测试网?.kI%_tq

1,写文本文件51Testing软件测试网K YrRU!W
Const ForReading = 1, ForWriting = 251Testing软件测试网6}/E$SU,Q S
   Dim fso, f51Testing软件测试网 a-Bf0s4J`~9dg
   set fso=createobject("scripting.filesystemobject")51Testing软件测试网%[K-YB!n H
   set f=fso.opentextfile("c:\testfile.txt",ForWriting,true)51Testing软件测试网x/^ d7['^d w
   f.write "hello"51Testing软件测试网9kANS1]?.E}
   f.write "world"51Testing软件测试网4Ft&q)rr M
   f.writeblanklines 2 ‘第二行写入空白行
O8Cs V9O/X0   f.writeline "worldhello"51Testing软件测试网h0p|^^C
   f.writeline "end"    ’写入并换行51Testing软件测试网P}(M'JK8~.T] QL
   f.close
3Q$A_VuD0  51Testing软件测试网#S'cm7We$Q({ip
2.读文本文件
3[.`:L z,l#p.J0Const ForReading = 1, ForWriting = 251Testing软件测试网7Rr5m'\#D J-u"i
   Dim fso, f51Testing软件测试网0m9xL1V8^zM
   set fso=createobject("scripting.filesystemobject")
Ns"iVd0   set f=fso.opentextfile("c:\testfile.txt",ForReading)
'Ikv-^d jE g0   readlinefile=f.readline '读取一行51Testing软件测试网2ct } PX+~)u0B
   readfileindex=f.read(5) ’读取5个字符51Testing软件测试网W.M$Ep,P
   readskipfile=f.ship(3)  ‘跳过3个字符开始读取
@$@I5Ng1l0   readskipfile2=f.read(2)’从第四个字符开始读取2个字符51Testing软件测试网-v R|1H+T} ]
   readallfile=f.readall  ‘读取所有字符51Testing软件测试网JA!ullUn
 
5F'N~;LM0  ’f.skipline
dS_-AT \_e0   readsecondline=f.readline  ‘读取第二行
Y5\_ C4u [x9i8e6d x0   f.colse51Testing软件测试网)q`^(j9@Vz0F
  
HK JH9W.Iq qx0  3.其他相关属性
z-\6W3Y;T0  Const ForReading = 151Testing软件测试网f4W7`9r:Fdf
   Dim fso, theFile, retstring
%FWLY['yAK0   Set fso = CreateObject("Scripting.FileSystemObject")51Testing软件测试网Y.mM]eY
   Set theFile = fso.OpenTextFile("c:\testfile.txt", ForReading, False)51Testing软件测试网 sdB,Z2v}c&d
   Do While theFile.AtEndOfStream<> True  ‘判断是不是文件流的结尾
7MO6SP(hcc0      retstring = theFile.ReadLine   ‘循环读取每一行
A#k/?q'u0   Loop51Testing软件测试网Mf7F |&Pe.q4I
   theFile.Close
.Cr.T$A Uw%c0   ReadEntireFile = retstring
*|4M3gw_0xR+},lU0  
d;U"F.C6oH \6C U0   Const ForReading = 1
6m BDTh.K.v&YN#n1~0   Dim fso, theFile, retstring51Testing软件测试网n6b'WX9J6C}IT bg
   Set fso = CreateObject("Scripting.FileSystemObject")
Jcz RM8YQ`0   Set theFile = fso.OpenTextFile(filespec, ForReading, False)51Testing软件测试网h tXo(P
   Do While theFile.AtEndOfLine<> True  判断是否行结尾51Testing软件测试网ovuqMsN
      retstring = theFile.Read(1)  ’循环读取没一个字符51Testing软件测试网;^_| ?J x9eH
   Loop51Testing软件测试网L8~-Ho2v f^_
   theFile.Close
B1O!F0K2R5Ch0 51Testing软件测试网)G4G8Br C5m @
 
$BlE#rA;}0 
%h9`P,Bw}D_0   Const ForReading = 1
$~a!X u7L![ MY{0   Dim fso, theFile, lines
8\)G:j4u+igH2z'w/RE0   Set fso = CreateObject("Scripting.FileSystemObject")51Testing软件测试网j0K hrm+cP
   Set f = fso.OpenTextFile(filespec, ForReading, False)51Testing软件测试网~*n.g-a~
   f.readall
z1N:gJ{$v5NM0   lines=f.line      ’返回行数
R?s~8w9v0   columns=f.column  ‘返回列数,即最长一行的字符个数51Testing软件测试网2E#Jn`y `a
  
;@T5ejCi7UV_5^'k e%o0  

g*[(^S2s5^(_'P%Q7^0

,s@G$tl`!h fR0
/VP_|~){~N0  51Testing软件测试网8t.Hz3Vehma{


TAG:

 

评分:0

我来说两句

Open Toolbar