不谋万世者,不足谋一时;不谋全局者,不足谋一域。君子敏于事而慎于言 新浪微薄:土司阿哈

关于文件的操作

上一篇 / 下一篇  2008-04-07 21:22:11

关于文件的操作

 51Testing软件测试网h&O;Z{Yx3mRV0b

创建文件51Testing软件测试网sN t6K;C6vl#l,U
dim fso, f
1e5\ f*oG|FgNJ.L0set fso = server.CreateObject("scrīpting.FileSystemObject")51Testing软件测试网L Z'o F#ItU
set f = fso.CreateTextFile("C:\test.txt", true) '第二个参数表示目标文件存在时是否覆盖51Testing软件测试网?4}7S;up){yR,FF
f.Write("写入内容")51Testing软件测试网1T,UG[2nv`7}0K!X
f.WriteLine("写入内容并换行")
K[)L0O C+J2v0f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)51Testing软件测试网 q\z6Z5\4e,i
f.Close()51Testing软件测试网 x;o km g
set f = nothing51Testing软件测试网&AVai `"jc(Y q
set fso = nothing51Testing软件测试网}~XO |4P1w
51Testing软件测试网)R[._&EnDgi{{%y
打开并读文件51Testing软件测试网 A f;z-P2?^#O~
dim fso, f51Testing软件测试网$w{p;m Jq$t
set fso = server.CreateObject("scrīpting.FileSystemObject")
nEf\H(aF*O:q0set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二个参数 1 表示只读打开,第三个参数表示目标文件不存在时是否创建
/@CAd{u C h0f.Skip(3) '将当前位置向后移三个字符51Testing软件测试网9i ox*agUx
f.SkipLine() '将当前位置移动到下一行的第一个字符,注意:无参数
`h4N3}#W$F/nF-@0response.Write f.Read(3) '从当前位置向后读取三个字符,并将当前位置向后移三个字符
'vc;K`;[0response.Write f.ReadLine() '从当前位置向后读取直到遇到换行符(不读取换行符),并将当前位置移动到下一行的第一个字符,注意:无参数51Testing软件测试网;`TAB)M&T
response.Write f.ReadAll() '从当前位置向后读取,直到文件结束,并将当前位置移动到文件的最后51Testing软件测试网D(eBWql8},t h
if f.atEndOfLine then51Testing软件测试网q!Rj8E}'j:U
    response.Write("一行的结尾!")51Testing软件测试网5Q!?6a;J;a-I*R
end if51Testing软件测试网n Vc0E'P s s:\
if f.atEndOfStream then
e/s9N6i,Hw A0    response.Write("文件的结尾!")51Testing软件测试网8GZn/B-C9N/Q ou
end if51Testing软件测试网.ky,sU8e&u:c/g;k5Y
f.Close()51Testing软件测试网-[&A.n P1k#L:Br
set f = nothing
@.~J%gI/F7cthU0set fso = nothing
Evr(O/_0p0V0
[j5c/_ ^'`WV!IMq0打开并写文件
-LT[_r-H0dim fso, f51Testing软件测试网 P9V&@DH2@t G
set fso = server.CreateObject("scrīpting.FileSystemObject")
,[;iW&O _(u B+jE?+n0set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二个参数 2 表示重写,如果是 8 表示追加
,d9qYx1f;\4xr0f.Write("写入内容")51Testing软件测试网Q/KK+tI:q Q8{ |G/F d
f.WriteLine("写入内容并换行")51Testing软件测试网@?.Vi~i
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
8mE,b3H~}F)V6o0f.Close()51Testing软件测试网4a sZg)T'X_8]1{
set f = nothing
j e7B Y4K0set fso = nothing51Testing软件测试网Ac*f7l*[
51Testing软件测试网.ig0Z;Vz*L
判断文件是否存在
)dykXrc0dim fso51Testing软件测试网2U3`5D CjI$ni1i
set fso = server.CreateObject("scrīpting.FileSystemObject")51Testing软件测试网T-Sdlc:`"~I^/S
if fso.FileExists("C:\test.txt") then
)R#G&~v#v H z'hD0    response.Write("目标文件存在")
LD4Km0g6c@7C0else51Testing软件测试网SN;^9nQG] O
    response.Write("目标文件不存在")
!|%D2l2K~8L0end if51Testing软件测试网'F*fl jB'r#D#o
set fso = nothing51Testing软件测试网!K&E V&[#| F#Bi c
51Testing软件测试网Qv:J&X {/F"u]
移动文件
"t5_*K;DM-r0dim fso
ptx@~;owd0set fso = server.CreateObject("scrīpting.FileSystemObject")
t~}J3I5Le/HL1ZlW0call fso.MoveFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同51Testing软件测试网 KVPe@,t.ho
set fso = nothing51Testing软件测试网%\,gU(U(~"c%B
51Testing软件测试网b(W%t;R2a&rF/p
复制文件51Testing软件测试网h\+}9e)](uj-b
dim fso51Testing软件测试网r1I7A#Qb&I? M
set fso = server.CreateObject("scrīpting.FileSystemObject")51Testing软件测试网Q/I;hf5q} CT
call fso.CopyFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同
0U?L$t8w(xe0set fso = nothing51Testing软件测试网2g*yd*S8m |%C

*[(wu1JiA0删除文件51Testing软件测试网 ZQ MjO@
dim fso
h2Sg gV c!ia0set fso = server.CreateObject("scrīpting.FileSystemObject")51Testing软件测试网N2D+ZD5a(Kd,|h
fso.DeleteFile("C:\test.txt")51Testing软件测试网 g4Gg.t*LXhwXw
set fso = nothing
;vm,CtO+a051Testing软件测试网9n nx#N asc&{x
创建文件夹51Testing软件测试网o;~.l x4M3xiw;h
dim fso
PytV*a0set fso = server.CreateObject("scrīpting.FileSystemObject")
l q!n8^@W0fso.CreateFolder("C:\test") '目标文件夹的父文件夹必须存在51Testing软件测试网.DANJ^ Ru
set fso = nothing51Testing软件测试网 DB;Rd:{?0N`?Cq/n

%R(|pp:IpV0判断文件夹是否存在51Testing软件测试网a7gK4f!pLj_
dim fso
2QpV1O'| v[0set fso = server.CreateObject("scrīpting.FileSystemObject")51Testing软件测试网,r'i%wa+u8o
if fso.FolderExists("C:\Windows") then
]LR,Fz h+w0    response.Write("目标文件夹存在")
tzEMp,]0else
I"L J0`0C _8w0    response.Write("目标文件夹不存在")51Testing软件测试网:[Y}JI6W u
end if
0[d[Z;z,]f@(x,y0set fso = nothing51Testing软件测试网k'~q#Ku cy6{
51Testing软件测试网zg(h6_8W2_d
删除文件夹
r0F | s#ME0dim fso51Testing软件测试网$K9^^v#E ax?
set fso = server.CreateObject("scrīpting.FileSystemObject")
}$m2StG0fso.DeleteFolder("C:\test") '文件夹不必为空51Testing软件测试网,HW Y1Z0R$Z5@[
set fso = nothing

TAG:

 

评分:0

我来说两句

Open Toolbar