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

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

|J;j2c b5|6mf01,写文本文件
3Q5B9{i#u0Const ForReading = 1, ForWriting = 251Testing软件测试网*d$m$t[Z:jvb$N sS
   Dim fso, f
n.m:k2K[hE O,m0   set fso=createobject("scripting.filesystemobject")51Testing软件测试网"\n)QVR oMF p
   set f=fso.opentextfile("c:\testfile.txt",ForWriting,true)51Testing软件测试网 B m8sT2hx&Ws)n!a
   f.write "hello"51Testing软件测试网,w8Loq(oj U H8g4}
   f.write "world"51Testing软件测试网:e].eO @;v
   f.writeblanklines 2 ‘第二行写入空白行51Testing软件测试网 z"zhVH {*aXSI
   f.writeline "worldhello"51Testing软件测试网k?wPMU L
   f.writeline "end"    ’写入并换行
,T2u#vD0R7Y0   f.close
*]0BFxN O q _0  51Testing软件测试网*a d-jdV\7mM
2.读文本文件
:w$s@Fl \x0Const ForReading = 1, ForWriting = 251Testing软件测试网c ub`r
   Dim fso, f51Testing软件测试网7d|-_4o m uO/[mrh
   set fso=createobject("scripting.filesystemobject")51Testing软件测试网,bV+vYg S VkN7y
   set f=fso.opentextfile("c:\testfile.txt",ForReading)
J"Rz|T.?SP0   readlinefile=f.readline '读取一行51Testing软件测试网0hg"YBQ{\)C
   readfileindex=f.read(5) ’读取5个字符51Testing软件测试网5U`T?rL!x+l l
   readskipfile=f.ship(3)  ‘跳过3个字符开始读取51Testing软件测试网 [Z+`c,Ke
   readskipfile2=f.read(2)’从第四个字符开始读取2个字符51Testing软件测试网^l|v.?H j m'd-a
   readallfile=f.readall  ‘读取所有字符51Testing软件测试网]L4Z#c3N&MI%w!f
 51Testing软件测试网'Vzi\0RS#N
  ’f.skipline
#X;{6XWEQD0   readsecondline=f.readline  ‘读取第二行51Testing软件测试网Vs0} {&jf
   f.colse
%t$y|/`*Q*W8{8H"n0  
;K5{0YS0r c'y0  3.其他相关属性
#qN;z|&zi4L%jQ0  Const ForReading = 1
r6B&lr/k e0   Dim fso, theFile, retstring51Testing软件测试网W+a*j K&B x R6Q
   Set fso = CreateObject("Scripting.FileSystemObject")51Testing软件测试网 y@5I(dOu3_.~
   Set theFile = fso.OpenTextFile("c:\testfile.txt", ForReading, False)
m D/Q \5J }+l;q0   Do While theFile.AtEndOfStream<> True  ‘判断是不是文件流的结尾
yEvY1Z6@w1V QB0      retstring = theFile.ReadLine   ‘循环读取每一行
[(iP#EF7hB_0   Loop
)m,~ij9`nmt"K0   theFile.Close51Testing软件测试网FP,@*iD8~ a!W
   ReadEntireFile = retstring51Testing软件测试网#[$W'N$t{7hN&L
  51Testing软件测试网Z"e(YbCN#H
   Const ForReading = 1
X ae6R4P p0   Dim fso, theFile, retstring51Testing软件测试网l)W"Mly
   Set fso = CreateObject("Scripting.FileSystemObject")51Testing软件测试网"g#G O]p*h'K/t6sT
   Set theFile = fso.OpenTextFile(filespec, ForReading, False)51Testing软件测试网Bp3WN.E
   Do While theFile.AtEndOfLine<> True  判断是否行结尾
TS|$I D4Q Ko i,g0      retstring = theFile.Read(1)  ’循环读取没一个字符
w8imx$V fox0   Loop51Testing软件测试网"N9~ Uu)O1jT
   theFile.Close51Testing软件测试网 C9y7r)h \N,Y_
 
:RZOC:[8v X;TU{#D8K*L!t0 
4d!m?M0t8J1d2D0 51Testing软件测试网EYNJ'k*I Wy:@-C
   Const ForReading = 1
'EMj H{7Z U0   Dim fso, theFile, lines
~_.G,`/xS1]0   Set fso = CreateObject("Scripting.FileSystemObject")
'o/j3U}N kLU tIM0   Set f = fso.OpenTextFile(filespec, ForReading, False)51Testing软件测试网gLoqa @U
   f.readall51Testing软件测试网si.wzNY7j7i*G V7D
   lines=f.line      ’返回行数
8s#`#se4K8mBj0   columns=f.column  ‘返回列数,即最长一行的字符个数51Testing软件测试网tB)s7v+s;Es]%bU O
  
GL*p2{{C[H-m0  51Testing软件测试网 F9Q8]0OY~

51Testing软件测试网~%^-v(Yz'rN


hXdMF0U B2cJ0  51Testing软件测试网{;f?4f,{^ g2Fe,a


TAG:

 

评分:0

我来说两句

Open Toolbar