我一定要做一个顶尖的软件测试工程师。。。加油。。加油。。加油

08-09-02读书笔记四

上一篇 / 下一篇  2008-09-02 23:15:08

VBscrīpt:处理文件(创建、写入、读取、删除)

2007-06-22 14:57:02 / 个人分类:QTP

1.创建文件 
_;qr%m"^_194265  创建空文本文件(有时被叫做“文本流”)有三种方法。

  第一种方法是用CreateTextFile方法。下面的示例示范了如何用CreateTextFile方法创建文本文件:

[VBscrīpt]
{CBG0rxL194265
Dim fso, f151Testing软件测试网%dFex0x~:bEf&}
Set fso = CreateObject("scrīpting.FileSystemObject")
kc"CvC$G8^ w194265Set f1 = fso.CreateTextFile("c:\testfile.txt", True)

  创建文本文件的第二种方法是,使用FileSystemObject对象的OpenTextFile方法,并设置ForWriting标志。

[VBscrīpt]51Testing软件测试网nfO~+]7]$H7ZBP
Dim fso, ts
"P4lTdo%n T194265Const ForWriting = 2
8xrk f S0hs^194265Set fso = CreateObject("scrīpting. FileSystemObject")
o,U6@3x8y#rf194265Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True)

  创建文本文件的第三种方法是,使用OpenAsTextStream方法,并设置ForWriting标志。要使用这种方法,使用下面的代码:

[VBscrīpt]51Testing软件测试网v~8}5h2wH
Dim fso, f1, ts
Z f YbL3j6@194265Const ForWriting = 2
(^MAu0C9tm/by194265Set fso = CreateObject("scrīpting.FileSystemObject")51Testing软件测试网|(J;Rc8iF#c!h
fso.CreateTextFile ("c:\test1.txt")
1tfH eVs194265Set f1 = fso.GetFile("c:\test1.txt")51Testing软件测试网|s%[6R(\
Set ts = f1.OpenAsTextStream(ForWriting, True)
u8bla AdA O;wQ194265
Ddt.Nj1942652.写入数据
o(@+o g~FS:]8af194265一旦创建了文本文件,使用下面的三个步骤向文件添加数据:

  打开文本文件。

  写入数据。

  关闭文件。

  要打开现有的文件,则使用 FileSystemObject 对象的 OpenTextFile 方法或 File 对象的 OpenAsTextStream 方法。

  要写数据到打开的文本文件,则根据下表所述任务使用 TextStream 对象的 Write、WriteLine 或 WriteBlankLines 方法。

任务方法
向打开的文本文件写数据,不用后续一个新行字符。Write
向打开的文本文件写数据,后续一个新行字符。WriteLine
向打开的文本文件写一个或多个空白行。WriteBlankLines

要关闭一个打开的文件,则使用TextStream对象的Close方法。

注意   新行字符包含一个或几个字符(取决于操作系统),以把光标移动到下一行的开始位置(回车/换行)。注意某些字符串末尾可能已经有这个非打印字符了。

下面的例子示范了如何打开文件,和同时使用三种写方法来向文件添加数据,然后关闭文件:

[VBscrīpt]51Testing软件测试网{ z} r"Z1n(kR
Sub CreateFile()51Testing软件测试网\+a!]vWla a
Dim fso, tf51Testing软件测试网3~` pO([5RHi
Set fso = CreateObject("scrīpting.FileSystemObject")51Testing软件测试网7P_4Zu&\1@2vH!F
Set tf = fso.CreateTextFile("c:\testfile.txt", True) ' 写一行,并带有一个新行字符。 tf.WriteLine("Testing1, 2, 3.") ' 向文件写三个新行字符。
(Ny,? [-ktp194265tf.WriteBlankLines(3) ' 写一行。51Testing软件测试网^f/{)_6V m9|*`
tf.Write ("This is a test.")51Testing软件测试网Q W)hiPO E
tf.Close51Testing软件测试网 tj vg'X4|-J
End Sub51Testing软件测试网4f2c0|h)fX!aG
51Testing软件测试网 `u'xjf"h,f
3.读取数据
'`X.?/GFcV0w1Uu194265   要从文本文件读取数据,则使用TextStream对象的ReadReadLineReadAll方法。下表描述了不同的任务应使用哪种方法。
任务方法
从文件读取指定数量的字符。Read
读取一整行(一直到但不包括新行字符)。ReadLine
读取文本文件的整个内容。ReadAll

  如果使用ReadReadLine方法,并且想跳过数据的特殊部分,则使用SkipSkipLine方法。read 方法的结果文本存在一个字符串中,该字符串可以显示在一个控件中,也可以用字符串函数(如LeftRightMid)来分析,连接等等。

下面的例子示范了如何打开文件,和如何写数据到文件中并从文件读取数据:

[VBscrīpt]51Testing软件测试网jW~5`%lkT9L
Sub ReadFiles51Testing软件测试网%hl:M@1| K
Dim fso, f1, ts, s51Testing软件测试网w%hpD bN*|
Const ForReading = 1
;J.v5w.? v&]194265Set fso = CreateObject("scrīpting.FileSystemObject")
$[kbCJ8dc{!g E194265Set f1 = fso.CreateTextFile("c:\testfile.txt", True)51Testing软件测试网n'_G5b.n$v fDkbQ
' 写入一行。
5?&N.{ b9u.q{#w194265Response.Write "Writing file <br>"
n B_ J%Z6a+|194265f1.WriteLine "Hello World"
I YrJ-o194265f1.WriteBlankLines(1)
}?&U^2lk$yWJ194265f1.Close51Testing软件测试网3@D&CYZdC
' 读取文件内容。51Testing软件测试网@XFZ w2AJ9a
Response.Write "Reading file <br>"51Testing软件测试网Yp [r`;F
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)51Testing软件测试网:w$D pv/QdbMGQ5F
s = ts.ReadLine
7L+pc9bg#| jM Qv194265Response.Write "File contents = '" & s & "'"51Testing软件测试网1XE3N5agF
ts.Close51Testing软件测试网t Hl/J(D
End Sub
]7F5`!x M+D194265
f)fq9k8E1942654.移动、复制、删除
l(cKILj"d%bk194265
FSO 对象模型各有两种方法移动、复制和删除文件,如下表所述。
任务方法
移动文件File.Move 或 FileSystemObject.MoveFile
复制文件File.Copy 或 FileSystemObject.CopyFile
删除文件File.Delete 或 FileSystemObject.DeleteFile

下面示例在驱动器 C 的根目录中创建一个文本文件,向其中写一些信息,然后把它移动到 \tmp 目录中,并在 \temp 中做一个备份,最后把它们从两个目录中删掉。

要运行下面的示例,需要先在驱动器 C 的根目录中创建 \tmp 和 \temp 目录:

[VBscrīpt]Sub ManipFiles Dim fso, f1, f2, s51Testing软件测试网\Dh*Pn+\!{+n{e
Set fso = CreateObject("scrīpting.FileSystemObject")51Testing软件测试网jv8m n@g7K
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)51Testing软件测试网d'NDI;r3Zk
Response.Write "Writing file <br>" ' 写入一行。
9{#p nZ9G O4i w NP194265f1.Write ("This is a test.") ' 关闭写入到的文件。
iqf#d'smy194265f1.Close Response.Write "Moving file to c:\tmp <br>" ' 获取到 C:\ 根目录中文件的句柄。 Set f2 = fso.GetFile("c:\testfile.txt") ' 将文件移到 \tmp 目录。51Testing软件测试网!qf']4g~_/K
f2.Move ("c:\tmp\testfile.txt")
q }Th"U194265Response.Write "Copying file to c:\temp <br>" ' 将文件复制到 \temp。
E'SE'~8y.Wa194265f2.Copy ("c:\temp\testfile.txt")
7`V9JAN'pQ7u194265Response.Write "Deleting files <br>" ' 获得文件当前位置的句柄。
)TU? ~I.l"{194265Set f2 = fso.GetFile("c:\tmp\testfile.txt") Set f3 = fso.GetFile("c:\temp\testfile.txt") ' 删除文件。
je4i3or5jPK194265f2.Delete f3.Delete
-D_9D N!uv194265Response.Write "All done!"
R$a!~AI194265End Sub

TAG:

 

评分:0

我来说两句

日历

« 2024-04-04  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 32842
  • 日志数: 38
  • 图片数: 1
  • 建立时间: 2008-07-28
  • 更新时间: 2013-02-16

RSS订阅

Open Toolbar