侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

VB FileSystemObject对象实例详解

技术  /  管理员 发布于 7年前   142

FileSystemObject对象被用来访问服务器上的文件系统。这个对象能够处理文件、文件夹和目录路径。用它来检索文件系统信息也是可能的。

下面的代码创建了一个文本文件,并写入了一些文本:

<%
dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
%>

FileSystemObject对象的属性和方法如下:

一、属性

Drives:返回计算机上关于所有Drive对象的集。
语法:
[drivecoll=]FileSystemObject.Drives

二、方法

Bulidpath:给已存在的路径增加一个名字。
CopyFile:从一处复制一个或多个文件到另一处。
CopyFolder:从一处复制一个或多个文件夹到另一处。
CreateFolder:创建一个新的文件夹。
CreateTextFile:创建一个文本文件并返回一个TextStream对象用来读写所创建的文本文件。
DeleteFile:删除一个或多个指定的文件。
DeleteFolder:删除一个或多个指定的文件夹。
DriveExists:检查指定的驱动器是否存在。
FileExists:检查指定的文件是否存在。
FolderExists:检查指定的文件夹是否存在。
GetAbsolutePathName:返回指定路径的完整路径。
GetBaseName:返回指定文件或文件夹的基本名。
GetDrive:返回指定路径的在驱动器的相应Drive对象。
GetDriveName:返回指定路径的驱动器名。
GetExtensionName:返回指定路径中最后部分的文件扩展名。
GetFile:返回一个关于指定路径的文件对象。
GetFileName:返回指定路径中最后部分的文件名或文件夹名。
GetFolder:返回一个关于指定路径的文件夹对象。
GetParentFolderName:返回指定路径中最后部分的父文件夹名。
GetSpecialFolder:返回Windows某个专门文件夹的路径。
GetTempName:返回一个随机生成的临时文件或文件夹。
MoveFile:将一个或多个文件从一个地方移动到另一地方。
MoveFolder:将一个或多个文件从一个地方移动到另一地方。
OpenTextFile:打开一个文件并返回一个TextStream对象用来读写所打开的文件。

BuildPath方法

BuildPath方法为已存在的路径增加一个名字。

一、语法

[newpath=]FileSystemObject.BuildPath(path,name)
参数说明:
path:必须的。路径。
name:所要增加的名字。

二、例子

<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.BuildPath("c:\mydocuments","test")
response.write(path)
set fs=nothing
%>

输出:

c:\mydocuments\test

CopyFile方法

CopyFile方法从一处复制一个或多个文件到另一处。

一、语法
FileSystemObject.CopyFile source,destination[,overwrite]
参数说明:
source:必须的。所要复制的文件。
destination:必须的。复制到的目的地。
overwrite:可选的。是个布尔值,它指出是否覆盖已存在的文件。True表示覆盖,False表示不覆盖。默认为True 。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFile "c:\mydocuments\web\*.htm","c:\webpages\"
set fs=nothing
%>

CopyFolder方法

CopyFolder方法一处复制一个或多个文件到另一处。

一、语法
FileSystemObject.CopyFolder source,destination[,overwrite]
参数说明:
source:必须的。所要复制的文件夹。
destination:必须的。复制到的目的地。
overwrite:可选的。是个布尔值,它指出是否覆盖已存在的文件。True表示覆盖,False表示不覆盖。默认为True 。

二、例子

<%
'copy all the folders in c:\mydocuments\web
'to the folder c:\webpages

dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "c:\mydocuments\web\*","c:\webpages\"
set fs=nothing
%>

<%
'copy only the folder test from c:\mydocuments\web
'to the folder c:\webpages

dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "c:\mydocuments\web\test","c:\webpages\"
set fs=nothing
%>

CreateFolder方法

CreateFolder方法创建一个新的文件夹。

一、语法

FileSystemObject.CreateFolder(name)
参数说明:
name:必须的。要创建的文件夹的名字。

二、例子

<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateFolder("c:\asp")
set f=nothing
set fs=nothing
%>

CreateTextFile方法

CreateTextFile方法在当前文件夹下创建一个新的文本文件,并返回一个TextStream对象用来读写这个新的文件。

一、语法

FileSystemObject.CreateTextFile(filename[,overwrite[,unicode]])
FolderObject.CreateTextFile(filename[,overwrite[,unicode]])
参数说明:
filename:必须的。所要创建的文件的名字。
overwrite:可选的。是一布尔值,以指出是否覆盖已存在的文件。True表示覆盖,False表示不覆盖。默认为True 。
unicode:可选的。为一布尔值,指出所创建的文件是Unicode文件还是ASCII文件。True为Unicode文件,False为ASCII文件。默认是False。

二、例子

FileSystemObject的例子:

<%
dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile("c:\somefile.txt")
tfile.WriteLine("Hello World!")
tfile.close
set tfile=nothing
set fs=nothing
%>

Folder对象的例子:

<%
dim fs,fo,tfile
Set fs=Server.CreateObject("Scripting.FileSystemObject") 
Set fo=fs.GetFolder("c:\test") 
Set tfile=fo.CreateTextFile("test.txt",false) 
tfile.WriteLine("Hello World!")
tfile.Close
set tfile=nothing
set fo=nothing
set fs=nothing
%>

DeleteFile方法

DeleteFile方法删除一个或多个指定的文件。
注意:如果试图删除不存在的文件将会发生错误。

一、语法

FileSystemObject.DeleteFile(filename[,force])
参数说明:
filename:必须的。所要删除的文件的名字。
force:可选的。一个布尔值,以表示是否删除只读文件。True为是,False为否。默认是False。

二、例子

<%
dim fs
Set fs=Server.CreateObject("Scripting.FileSystemObject") 
fs.CreateTextFile("c:\test.txt",True)
if fs.FileExists("c:\test.txt") then
  fs.DeleteFile("c:\test.txt")
end if
set fs=nothing
%>

DeleteFolder方法

DeleteFolder方法DeleteFile方法删除一个或多个指定的文件夹。
注意:如果试图删除不存在的文件夹将会发生错误。

一、语法

FileSystemObject.DeleteFolder(foldername[,force])
参数说明:
foldername:必须的。所要删除的文件的名字。
force:可选的。一个布尔值,以表示是否删除只读文件夹。True为是,False为否。默认是False。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FolderExists("c:\temp") then
  fs.DeleteFolder("c:\temp")
end if
set fs=nothing
%>

DriveExists方法

DriveExists方法返回一个布尔值表明指定的驱动器是否存在。True为存在,False为否。

一、语法
FileSystemObject.DriveExists(drive)
参数说明:
drive:必须的。一个驱动器符或一完整的路径描述。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.DriveExists("c:")=true then
   response.write("Drive c: exists!")
else
  response.write("Drive c: does not exist.")
end If
set fs=nothing
%>

FileExists方法

FileExists方法返回一个布尔值表明指定的文件是否存在。True为存在,False为否。

一、语法
FileSystemObject.FileExists(filename)
参数说明:
filename:必须的。所要检查的文件的名字。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists("c:\asp\introduction.asp")=true then
  response.write("File c:\asp\introduction.asp exists!")
else
   response.write("File c:\asp\introduction.asp does not exist!")
end if
set fs=nothing
%>

FolderExists方法
FolderExists方法返回一个布尔值表明指定的文件夹是否存在。True为存在,False为否。

一、语法

FileSystemObject.FolderExists(foldername)
参数说明:
foldername:必须的。所要检查的文件夹的名字。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FolderExists("c:\asp")=true then
  response.write("Folder c:\asp exists!")
else
  response.write("Folder c:\asp does not exist!")
end if
set fs=nothing
%>

GetAbsolutePathName方法
GetAbsolutePathName方法返回关于指定路径的完整路径(将指定路径转换为绝对路径)。

一、语法
FileSystemObject.GetAbsolutePathName(path)
参数说明:
path:必须的。要转换为绝对路径的路径。

二、例子
假设当前目录是 c:\temp\test: 
例1

<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.GetAbsolutePathName("c:")
response.write(path)
%>

输出:

c:\temp\test

例 2

<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.GetAbsolutePathName("mydoc.txt")
response.write(path)
%>

输出:

c:\temp\test\mydoc.txt

例 3

<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.GetAbsolutePathName("private\mydoc.txt")
response.write(path)
%>

输出:

c:\temp\test\private\mydoc.txt

GetBaseName方法

GetBaseName方法返回指定路径中文件或文件夹的基本名。

一、语法
FileSystemObject.GetBaseName(path)
参数说明:
path:必须的。文件或文件夹的路径。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write(fs.GetBaseName("c:\winnt\cursors\3dgarro.cur"))
set fs=nothing
%>

输出:

3dgarro

GetDrive方法

GetDrive方法返回一个由drivespec参数指定的Drive对象。

一、语法
FileSystemObject.GetDrive(drivespec)
参数说明:
drivespec:必须的。可以是一个驱动器符©,或后跟冒号的驱动器符(c:),或后跟冒号和路径分隔符的驱动器符(c:\),或网络共享说明(\\computer2\share1)。

二、例子

<%
dim fs,d
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:\")
set fs=nothing
%>

GetDriveName方法

GetDriveName方法返回一个包含指定路径的驱动器的名字的字符串。

一、语法
FileSystemObject.GetDriveName(path)
参数说明:
path:必须的。指定的路径。

二、例子

<%
dim fs,dname
set fs=Server.CreateObject("Scripting.FileSystemObject")
dname=fs.GetDriveName("c:\test\test.htm")
Response.Write(dname)
set fs=nothing
%>

输出:

c:

GetExtensionName方法

GetExtensionName方法返回一个包含指定路径中最后部分的文件的文件扩展名的字符串。

一、语法
FileSystemObject.GetExtensionName(path)
参数说明:
path:必须的。指定的路径。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write(fs.GetExtensionName("c:\test\test.htm"))
set fs=nothing
%>

输出:

htm

GetFile方法

GetFile方法返回关于指定路径的一个File对象。

一、语法
FileSystemObject.GetFile(path)
参数说明:
path:必须的。关于特定文件的路径。

二、例子

<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFile("c:\test\test.htm")
Response.Write("The file was last modified on: ")
Response.Write(f.DateLastModified)
set f=nothing
set fs=nothing
%>

输出:

The file was last modified on 01/01/20 4:23:56 AM

GetFileName方法

GetFileName方法返回一个包含指定路径中最后部分的文件或文件夹的名字的字符串。

一、语法
FileSystemObject.GetFileName(path)
参数说明:
path:必须的。关于特定文件或文件夹的路径。

二、例子

<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.getfilename("c:\test\test.htm")
response.write(p)
set fs=nothing
%>

输出:

test.htm

<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.getfilename("c:\test\")
response.write(p)
set fs=nothing
%>

输出:

test

GetFolder方法

GetFolder方法返回关于指定路径的一个Folder对象。

一、语法
FileSystemObject.GetFolder(path)
参数说明:
path:必须的。关于一特定文件夹的路径。

二、例子

<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFolder("c:\test\")
Response.Write("The folder was last modified on: ")
Response.Write(f.DateLastModified)
set f=nothing
set fs=nothing
%>

输出:

The folder was last modified on 01/01/20 4:23:56 AM

GetParentFolderName方法

GetParentFolderName方法返回指定路径中最后部分的父文件夹的名字。

一、语法

FileSystemObject.GetParentFolderName(path)
参数说明:
path:必须的。要返回其父文件夹名字的文件或文件夹的路径。

二、例子

<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur")
Response.Write(p)
set fs=nothing
%>

输出:

c:\winnt\cursors

GetSpecialFolder方法

GetSpecialFolder方法返回关于某Windows特定文件夹的路径。

一、语法
FileSystemObject.GetSpecialFolder(foldername) 
参数说明:
foldername:必须的。
foldername取值说明:
0=WindowsFolder(包含被windows操作系统安装的文件);
1=SystemFolder(包含库、字体和设备驱动程序)
2=TemporaryFolder(用来存储临时文件)

二、例子

<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
set p=fs.GetSpecialFolder(1)
Response.Write(p)
set p=nothing
set fs=nothing
%>

输出:

C:\WINNT\system32

GetTempName方法

GetTempName方法返回一个随机生成的临时文件或文件夹。

一、语法

FileSystemObject.GetTempName

二、例子

<%
dim fs,tfolder,tname, tfile
Set fs=Server.CreateObject("Scripting.FileSystemObject") 
Set tfolder=fs.GetSpecialFolder(2)
tname=fs.GetTempName
Set tfile=tfolder.CreateTextFile(tname) 
Response.write (tfile)
%>

输出:

trb2007.tmp

MoveFile方法

MoveFile方法把一个或多个文件从一处移动到另一处。

一、语法
FileSystemObject.MoveFile source,destination
参数说明:
source:必须的。要被移动的文件的路径。
destination:必须的。所要移动到的位置。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.MoveFile "c:\web\*.gif","c:\images\"
set fs=nothing
%>

MoveFolder方法

MoveFolder方法把一个或多个文件夹从一处移动到另一处。

一、语法
FileSystemObject.MoveFolder source,destination
参数说明:
source:必须的。要被移动的文件夹的路径。
destination:必须的。所要移动到的位置。

二、例子

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.MoveFolder "c:\test\web\","c:\windows\"
set fs=nothing
%>

OpenTextFile方法

OpenTextFile方法打开一个指定的文件并返回一个TextStream对象以用来访问这个文件。

一、语法
FileSystemObject.OpenTextFile(fname,mode,create,format)
参数说明:
fname:必须的。要打开的文件的名字。
mode:可选的。以什么方式打开。1=ForReading(以只读方式打开),2=ForWriting (以写方式打开),8=ForAppending(以添加方式打开,写入的内容将添加到文件末尾)。
create:可选的。设置如果所打开的文件不存在是否创建该文件。True为是,False为否。默认是False。
format:可选的。文件的格式。0=TristateFalse(以ASCII格式打开,这是默认的),-1=TristateTrue(以Unicode格式打开),-2=TristateUseDefault (以系统默认方式打开)

二、例子

<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile(Server.MapPath("testread.txt"),8,true)
f.WriteLine("This text will be added to the end of file")
f.Close
set f=Nothing
set fs=Nothing
%>  

Property

属性 Description
描述 
Attributes 
Sets or returns the attributes of a specified file
设置或返回指定文件的属性 
DateCreated 
Returns the date and time when a specified file was created
返回指定文件建立的日期和时间 
DateLastAccessed
Returns the date and time when a specified file was last accessed
返回指定文件最后被访问的日期和时间 
DateLastModified
Returns the date and time when a specified file was last modified
返回指定文件最后被修改的日期和时间 
Drive
 Returns the drive letter of the drive where a specified file or folder resides
返回指定文件或文件夹所处的盘符的盘符号 
Name
 Sets or returns the name of a specified file
设置或返回指定文件的名字 
ParentFolder
 Returns the folder object for the parent of the specified file
返回指定文件的父文件夹 
Path 
Returns the path for a specified file
返回一个指定文件的路径 
ShortName
 Returns the short name of a specified file (the 8.3 naming convention)
返回一个指定文件的短名 (根据8.3 命名规则) 
ShortPath
 Returns the short path of a specified file (the 8.3 naming convention)
返回一个指定文件的短路径 (根据8.3 命名规则) 
Size
 Returns the size, in bytes, of a specified file
返回指定文件所包含的字节数 
Type
 Returns the type of a specified file
返回指定文件的类型

Methods
方法
Method
方法 Description
描述 
Copy
Copies a specified file from one location to another
将本机上的文件复制到异地机子上 
Delete Deletes a specified file
删除指定文件 
Move
Moves a specified file from one location to another
将本机上的文件移动到异地机子上 
OpenAsTextStream
Opens a specified file and returns a TextStream object to access the file
打开指定文件返回一个TextStream对象

这篇文章就介绍到这了,建议继续查看下面的相关文章就行深入学习。


  • 上一条:
    VB中使用WMI获取系统硬件和软件有关信息
    下一条:
    VB实现的《QQ美女找茬游戏》作弊器实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(0个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客