前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Scripting.FileSystemObject详解

Scripting.FileSystemObject详解

作者头像
全栈程序员站长
发布2022-09-14 11:20:10
1.5K0
发布2022-09-14 11:20:10
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

FSO是FileSystemObject 或 Scripting.FileSystemObject 的缩写,为 IIS 内置组件,用于操作磁盘、文件夹或文本文件。FSO 的对象、方法和属性非常的多,这里用示例的方式列出常用的,注意:《VBScript 语言参考》或《JScript 语言参考》中的:《FileSystemObject 用户指南》和《Scripting 运行时库参考》便是微软给出的 FileSystemObject 完整参考。

FSO 不能操作二进制文件,要操作二进制文件,使用:ADODB.Stream。

创建文件 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 = nothing set 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 then response.Write(“一行的结尾!”) end if if f.atEndOfStream then response.Write(“文件的结尾!”) end if f.Close() set f = nothing set 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 = nothing set fso = nothing

判断文件是否存在 set fso = server.CreateObject(“Scripting.FileSystemObject”) if fso.FileExists(“C:\test.txt”) then response.Write(“目标文件存在”) else response.Write(“目标文件不存在”) end if set 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”) then response.Write(“目标文件夹存在”) else response.Write(“目标文件夹不存在”) end if set 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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158995.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档