1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 易语言FileSystemObject通过FSO对象创建删除写入打开移动复制判断文件

易语言FileSystemObject通过FSO对象创建删除写入打开移动复制判断文件

时间:2020-01-10 02:30:20

相关推荐

易语言FileSystemObject通过FSO对象创建删除写入打开移动复制判断文件

Scripting.FileSystemObject 为 IIS 内置组件,用于操作磁盘、文件夹或文本文件,通常简写为FSO,在ASP时代,对它的调用操控较多,其实易语言通过对象这个类也可以完全实现,由于是微软的东西,我们在删除文件,创建时,成功率还是比较高的,这里给出几个示范。

删除

首先,我们创建Object,Delete两个变量,它的类型是对象,源码:

先在C盘写一个文件

写到文件 (“c:\a.exe”, 到字节集 (“”))

.版本 2.局部变量 Object, 对象.局部变量 Delete, 对象.如果 (Object.创建 (“scripting.FileSystemObject”, ))Delete = Object.读对象型属性 (“GetFile”, “c:\a.exe”)Delete.方法 (“Delete”, ).否则

打开并写文件写入内容

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

易语言实现

.版本 2.局部变量 Object, 对象.局部变量 f, 对象Object.创建 (“Scripting.FileSystemObject”, )f = Object.读对象型属性 (“CreateTextFile”, “C:\test.txt”)f.方法 (“Write”, “”) ' 写入内容 f.方法 (“WriteLine”, “”) ' 写入内容并换行f.方法 (“WriteBlankLines”, 3Object.清除 ()f.清除 ()

FileSystemObject实例代码 基于上面的转化,我们可以实现以下文件操作

创建文件

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

打开并读文件

set fso = server.CreateObject("Scripting.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 thenresponse.Write("一行的结尾!")end ifif f.atEndOfStream thenresponse.Write("文件的结尾!")end iff.Close()set f = nothingset fso = nothing

打开并写文件

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

判断文件是否存在

set fso = server.CreateObject("Scripting.FileSystemObject")if fso.FileExists("C:\test.txt") thenresponse.Write("目标文件存在")elseresponse.Write("目标文件不存在")end ifset fso = nothing

移动文件

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

复制文件

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

删除文件

set fso = server.CreateObject("Scripting.FileSystemObject")fso.DeleteFile("C:\test.txt")set fso = nothing

创建文件夹

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

判断文件夹是否存在

set fso = server.CreateObject("Scripting.FileSystemObject")if fso.FolderExists("C:\Windows") thenresponse.Write("目标文件夹存在")elseresponse.Write("目标文件夹不存在")end ifset fso = nothing

删除文件夹

set fso = server.CreateObject("Scripting.FileSystemObject")fso.DeleteFolder("C:\test") '文件夹不必为空set fso = nothing

检测驱动器C盘是否存在

Set fso = Server.CreateObject("Scripting.FileSystemObject")fso.DriveExists("c:")

获取文件路径的驱动器名

Set fso=Server.CreateObject("Scripting.FileSystemObject")p=fso.GetDriveName(Server.MapPath("aqa33"))Response.Write("驱动器名称是:" & p)set fs=nothing

取得某个指定的路径的父文件夹的名称

Set fso=Server.CreateObject("Scripting.FileSystemObject")p=fso.GetParentFolderName(Server.MapPath("aqa331.asp"))Response.Write("父文件夹名称是:" & p)set fs=nothing

取得指定路径中的最后一个成分的文件扩展名

Set fs=Server.CreateObject("Scripting.FileSystemObject")Response.Write(fs.GetExtensionName(Server.MapPath("aqa33.asxd")))set fs=nothing

取得指定路径中的最后一个成分的文件名

Set fs=Server.CreateObject("Scripting.FileSystemObject")Response.Write(fs.GetFileName(Server.MapPath("aqa33.asxd")))set fs=nothing

返回在指定的路径中文件或者文件夹的基本名称。

Set fso=Server.CreateObject("Scripting.FileSystemObject")Response.Write(fso.GetBaseName("c:\windows\cursors\abc.cur"))Response.Write("")Response.Write(fso.GetBaseName("c:\windows\cursors\"))Response.Write("")Response.Write(fso.GetBaseName("c:\windows\"))set fso=nothing

参考:易语言FileSystemObject通过对象创建删除写入文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。