loading ...
loading...

2008-07-14 | qtp对文件的简单操作

分享
标签: server  write  test  nothing  fset 

qtp对文件的简单操作
觉得还不错,新接触vbs的同行可以看看(转自zhuyuancan的空间,谢谢作者 )


创建文件
dim fso, f
set fso = server.CreateObject("scrīpting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) '第二个参数表示目标文件存在时是否覆盖
f.Write("写入内容")
f.WriteLine("写入内容并换行")
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
f.Close()
set f = nothing
set fso = nothing

打开并读文件
dim fso, f
set fso = server.CreateObject("scrīpting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二个参数 1 表示只读打开,第三个参数表示目标文件不存在时是否创建
f.Skip(3) '将当前位置向后移三个字符
f.SkipLine() '将当前位置移动到下一行的第一个字符,注意:无参数
response.Write f.Read(3) '从当前位置向后读取三个字符,并将当前位置向后移三个字符
response.Write f.ReadLine() '从当前位置向后读取直到遇到换行符(不读取换行符),并将当前位置移动到下一行的第一个字符,注意:无参数
response.Write f.ReadAll() '从当前位置向后读取,直到文件结束,并将当前位置移动到文件的最后
if f.atEndOfLine then
    response.Write("一行的结尾!")
end if
if f.atEndOfStream then
    response.Write("文件的结尾!")
end if
f.Close()
set f = nothing
set fso = nothing

打开并写文件
dim fso, f
set fso = server.CreateObject("scrīpting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二个参数 2 表示重写,如果是 8 表示追加
f.Write("写入内容")
f.WriteLine("写入内容并换行")
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
f.Close()
set f = nothing
set fso = nothing

判断文件是否存在
dim fso
set fso = server.CreateObject("scrīpting.FileSystemObject")
if fso.FileExists("C:\test.txt") then
    response.Write("目标文件存在")
else
    response.Write("目标文件不存在")
end if
set fso = nothing

移动文件
dim fso
set fso = server.CreateObject("scrīpting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同
set fso = nothing

复制文件
dim fso
set fso = server.CreateObject("scrīpting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同
set fso = nothing

删除文件
dim fso
set fso = server.CreateObject("scrīpting.FileSystemObject")
fso.DeleteFile("C:\test.txt")
set fso = nothing

创建文件夹
dim fso
set fso = server.CreateObject("scrīpting.FileSystemObject")
fso.CreateFolder("C:\test") '目标文件夹的父文件夹必须存在
set fso = nothing

判断文件夹是否存在
dim fso
set fso = server.CreateObject("scrīpting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
    response.Write("目标文件夹存在")
else
    response.Write("目标文件夹不存在")
end if
set fso = nothing

删除文件夹
dim fso
set fso = server.CreateObject("scrīpting.FileSystemObject")
fso.DeleteFolder("C:\test") '文件夹不必为空
set fso = nothing

删除文件夹

'path为要删除目录路径
sub delfolder(path)
Dim WshShell, oExec
Set WshShell = CreateObject("Wscrīpt.Shell")
Set ōExec = WshShell.Exec("cmd /c del /s /q "+path)
set ōexec = wshshell.exec("cmd /c rd /s /q "+path)
end sub

分享 分享 |  评论 (0) |  阅读 (?)  |  固定链接 |  类别 (QTP) |  发表于 14:03
搜狐博客温馨提示:搜狐博客官方不会要求参加活动的各位博友缴纳任何的手续费用。请勿轻信留言、评论中的中奖信息,更不要拨打陌生电话及向陌生帐户汇款,谨防受骗!识别更多网络骗术,请 点击查看详情
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
 
  *中国人爱国心,搜狗输入法爱国主题皮肤下载>>
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复