致力于自动化测试技术,性能测试技术的研究,测试技术培训以及项目实施,做一个技术与实施的主导者。

vbs操作txt文本文件常用方法(函数)

上一篇 / 下一篇  2012-11-04 20:59:23 / 天气: 晴朗 / 心情: 高兴 / 个人分类:Quick Test Professional

51Testing软件测试网*QVH(oz o&mU!f

------------------------------------51Testing软件测试网s'E+E_@.K0uq

51Testing软件测试网` ar5Vw&m!MR6i?

'creat by 席飞剑(小席老师)51Testing软件测试网.f'h?y%D!C&M

51Testing软件测试网 npl#J2dy

'操作文本文件,操作fso对象(文件对象操作)

O6twY/z0

$U%}.BjHhRso0------------------------------------51Testing软件测试网o c+|p(i^ |'nR

axk+zE0创建文件51Testing软件测试网aM S&n5rn7R
dim fso, f
*er/iadQ8l1[,~Y0set fso = server.CreateObject("Scripting.FileSystemObject")
1O,K+MoZ]XA3?2iQ!WN0set f = fso.CreateTextFile("C:\test.txt", true) '第二个参数表示目标文件存在时是否覆盖51Testing软件测试网 o;rq(Fj6\w/U0}
f.Write("写入内容")
KO4L/Q7m5Hk1vhcw'O0f.WriteLine("写入内容并换行")51Testing软件测试网.Cn/g3[H]rV d_ g
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
1Vq2`K](e0f.Close()51Testing软件测试网'z$y3TTf qP)q
set f = nothing
Q8u'v ]0I|A)L-r:t0set fso = nothing51Testing软件测试网+qB a?_ |5|

51Testing软件测试网5N m-_&Qp6DV

-----------------------------------------------------------51Testing软件测试网ScnP;L q

2N8S^:Iu@:H0打开并读文件
Iq"Vv Z4Fim0dim fso, f
^q2e5WMr#@%aP0set fso = server.CreateObject("Scripting.FileSystemObject")51Testing软件测试网 t`1Z@YO%W
set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二个参数 1 表示只读打开,第三个参数表示目标文件不存在时是否创建51Testing软件测试网T$xA$CY8p N%DO.p
f.Skip(3) '将当前位置向后移三个字符
.?&e`-R+_pv0f.SkipLine() '将当前位置移动到下一行的第一个字符,注意:无参数
(g/LmT F3r0response.Write f.Read(3) '从当前位置向后读取三个字符,并将当前位置向后移三个字符51Testing软件测试网$joD*{5z9Dr
response.Write f.ReadLine() '从当前位置向后读取直到遇到换行符(不读取换行符),并将当前位置移动到下一行的第一个字符,注意:无参数51Testing软件测试网9Tmh*YPL*Cx:\}
response.Write f.ReadAll() '从当前位置向后读取,直到文件结束,并将当前位置移动到文件的最后51Testing软件测试网 rpJs`+x9D6HB#jZ
if f.atEndOfLine then51Testing软件测试网r:mmQs qz
    response.Write("一行的结尾!")51Testing软件测试网1p;G)sA \ \
end if51Testing软件测试网:K rG&a9S}-sj
if f.atEndOfStream then51Testing软件测试网 ] iX;xT!o z-^f{
    response.Write("文件的结尾!")51Testing软件测试网)b ? C1gzI
end if
$q\lm;l&T.G*j,h7Pm6P0f.Close()51Testing软件测试网[] fs3_!Z"\
set f = nothing51Testing软件测试网TkQ'}K$Y;_M"_ BT
set fso = nothing

)a l9S2}/w%CR"}B0

5T"\R5i%HU$m0-----------------------------------------------------------

/x.d!l lA?l!T051Testing软件测试网0G k4Y6]9m

打开并写文件
{'D^ q)pgoh0dim fso, f
rWY8~@V0set fso = server.CreateObject("Scripting.FileSystemObject")51Testing软件测试网)a'Q$Wv$| O'\/z8n
set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二个参数 2 表示重写,如果是 8 表示追加
6Te |jg(a)Kk0f.Write("写入内容")51Testing软件测试网;k}S6{!~ i3}N_p,t!u
f.WriteLine("写入内容并换行")51Testing软件测试网Oa-\2O ICi/In
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)51Testing软件测试网o7Z~5q'D{8^ gg
f.Close()
p]y }^bO0set f = nothing
z6x Ls'H h Tg0set fso = nothing51Testing软件测试网[)B)yX~x7gm u

51Testing软件测试网oEir8JY

-----------------------------------------------------------51Testing软件测试网Gy{5t @@

R0lQ$|(~6zn7f0判断文件是否存在
0q0B l6Eo9pi Q.I0dim fso
@`(^9f1z$u0set fso = server.CreateObject("Scripting.FileSystemObject")
:x9z0}@7S,iZ#d5O2r0if fso.FileExists("C:\test.txt") then
l'?k-z9J0    response.Write("目标文件存在")51Testing软件测试网K D5A:VoU
else
/M(|`mL!Ua0    response.Write("目标文件不存在")
^2z)]T"Z$B f0end if
J)j/m.|AV-il4s0set fso = nothing51Testing软件测试网P-u'oZI;_Z)w

51Testing软件测试网/w5jF?_ i8Zq

-----------------------------------------------------------

8?H9W8X#V+i)Z4T0

L S*H9q!rG1d)S.M%K0移动文件51Testing软件测试网tlO p)@NF.S
dim fso
2ZlJ Pwk0set fso = server.CreateObject("Scripting.FileSystemObject")51Testing软件测试网u!|A0X6\W/O3T}_
call fso.MoveFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同51Testing软件测试网p&C.Tr/NHC
set fso = nothing

.{!]V+c)b.g051Testing软件测试网$['q5nz\4TfE

-----------------------------------------------------------51Testing软件测试网gG#r6gn?Jre

51Testing软件测试网 PWxN-x L

复制文件
i,C*Y%u!X-DT0dim fso51Testing软件测试网 q6FmF7O
set fso = server.CreateObject("Scripting.FileSystemObject")
C2Q f i6T(S b'qc(T U0call fso.CopyFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同51Testing软件测试网R%O^)KKl(z"WU g
set fso = nothing

Zlw*G x051Testing软件测试网&QRD&^,Cl B

-----------------------------------------------------------51Testing软件测试网r+g/WHx

Mynd$T0删除文件51Testing软件测试网\ K)UE7Sy7K[+j,U
dim fso
t,?"jDZ)zl.m0set fso = server.CreateObject("Scripting.FileSystemObject")51Testing软件测试网8K~#ZCX2x
fso.DeleteFile("C:\test.txt")51Testing软件测试网y\rvY FW I
set fso = nothing

z-K%}9^^([IX9e[051Testing软件测试网j(] q[t^D M6W

-----------------------------------------------------------51Testing软件测试网Ye8j$ET#rN|

51Testing软件测试网d#D"k1y d Gn f;BS:FK

创建文件夹51Testing软件测试网Q3E9V9B?3ZFug
dim fso51Testing软件测试网7Z;I l+{!e1We+r,B1x
set fso = server.CreateObject("Scripting.FileSystemObject")
~v'| [Z0fso.CreateFolder("C:\test") '目标文件夹的父文件夹必须存在
3Dd(Q~.v kk9u0set fso = nothing51Testing软件测试网wu*t%\ C{

51Testing软件测试网'?2Z%L Kf7Br

-----------------------------------------------------------

SI.Gc}BxHpf0

k5pan#i$[0判断文件夹是否存在51Testing软件测试网VXgJlWl.X
dim fso
!D"oFEO!u f_7\0set fso = server.CreateObject("Scripting.FileSystemObject")51Testing软件测试网BH/X3bIk:O
if fso.FolderExists("C:\Windows") then
y o5cS"V K0    response.Write("目标文件夹存在")51Testing软件测试网A'a*k3B&q o4]x
else51Testing软件测试网 TX+P$RAJLrX!AUo
    response.Write("目标文件夹不存在")51Testing软件测试网v/O$Yx$c9YD
end if51Testing软件测试网} C@"u ud
set fso = nothing51Testing软件测试网GE"Os1efS

51Testing软件测试网#UP!?GWG

-----------------------------------------------------------

_[#b J,@2F&q:q051Testing软件测试网1YWq:U:Q/}w

删除文件夹51Testing软件测试网6c$S g6D5m;b,[/c_1D3[
dim fso
u x'kr[0set fso = server.CreateObject("Scripting.FileSystemObject")
2St+b^D0`+n"X0fso.DeleteFolder("C:\test") '文件夹不必为空51Testing软件测试网)P+[}@0w]
set fso = nothing51Testing软件测试网$@9pr.\#C$wE'[

O9[;Z"c$UC0

TAG:

 

评分:0

我来说两句

vprince

vprince

6年软件测试经验,TIB自动化测试工作室核心成员,ATF框架核心设计和开发人员,熟悉软件自动化测试、性能测试,多年从事软件项目的自动化测试和性能测试,对自动化测试的框架设计开发、框架搭建以及实施有较为丰富的实战经验。 目前关注开源自动化测试领域、 基于Selenium构建Web自动化测试框架,为多家企业进行自动化测试培训、实施自动化测试项目。

日历

« 2024-05-07  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 67509
  • 日志数: 49
  • 建立时间: 2009-09-09
  • 更新时间: 2012-12-14

RSS订阅

Open Toolbar