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

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

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

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

一、属性

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

二、方法

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

BuildPath方法

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

一、语法

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

二、例子

<%
dim fs,path
set fs=Server.Create (\" ing.FileSystem \")
path=fs.BuildPath(\"c:\\mydocuments\",\"test\")
response.write(path)
set fs=nothing
%>

输出:

c:\\mydocuments\\test

CopyFile方法

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

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
fs.CopyFile \"c:\\mydocuments\\web\\*.htm\",\"c:\\webpages\\\"
set fs=nothing
%>

CopyFolder方法

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

一、语法
FileSystem .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.Create (\" ing.FileSystem \")
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.Create (\" ing.FileSystem \")
fs.CopyFolder \"c:\\mydocuments\\web\\test\",\"c:\\webpages\\\"
set fs=nothing
%>

CreateFolder方法

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

一、语法

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

二、例子

<%
dim fs,f
set fs=Server.Create (\" ing.FileSystem \")
set f=fs.CreateFolder(\"c:\\asp\")
set f=nothing
set fs=nothing
%>

CreateTextFile方法

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

一、语法

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

二、例子

FileSystem 的例子:

<%
dim fs,tfile
set fs=Server.Create (\" ing.FileSystem \")
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.Create (\" ing.FileSystem \") 
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方法删除一个或多个指定的文件。
注意:如果试图删除不存在的文件将会发生错误。

一、语法

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

二、例子

<%
dim fs
Set fs=Server.Create (\" ing.FileSystem \") 
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方法删除一个或多个指定的文件夹。
注意:如果试图删除不存在的文件夹将会发生错误。

一、语法

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
if fs.FolderExists(\"c:\\temp\") then
  fs.DeleteFolder(\"c:\\temp\")
end if
set fs=nothing
%>

DriveExists方法

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

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
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为否。

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
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为否。

一、语法

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
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方法返回关于指定路径的完整路径(将指定路径转换为绝对路径)。

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

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

<%
dim fs,path
set fs=Server.Create (\" ing.FileSystem \")
path=fs.GetAbsolutePathName(\"c:\")
response.write(path)
%>

输出:

c:\\temp\\test

例 2

<%
dim fs,path
set fs=Server.Create (\" ing.FileSystem \")
path=fs.GetAbsolutePathName(\"mydoc.txt\")
response.write(path)
%>

输出:

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

例 3

<%
dim fs,path
set fs=Server.Create (\" ing.FileSystem \")
path=fs.GetAbsolutePathName(\"private\\mydoc.txt\")
response.write(path)
%>

输出:

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

Get Name方法

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

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
Response.Write(fs.Get Name(\"c:\\winnt\\cursors\\3dgarro.cur\"))
set fs=nothing
%>

输出:

3dgarro

GetDrive方法

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

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

二、例子

<%
dim fs,d
set fs=Server.Create (\" ing.FileSystem \")
set d=fs.GetDrive(\"c:\\\")
set fs=nothing
%>

GetDriveName方法

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

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

二、例子

<%
dim fs,dname
set fs=Server.Create (\" ing.FileSystem \")
dname=fs.GetDriveName(\"c:\\test\\test.htm\")
Response.Write(dname)
set fs=nothing
%>

输出:

c:

GetExtensionName方法

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

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
Response.Write(fs.GetExtensionName(\"c:\\test\\test.htm\"))
set fs=nothing
%>

输出:

htm

GetFile方法

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

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

二、例子

<%
dim fs,f
set fs=Server.Create (\" ing.FileSystem \")
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方法返回一个包含指定路径中最后部分的文件或文件夹的名字的字符串。

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

二、例子

<%
dim fs,p
set fs=Server.Create (\" ing.FileSystem \")
p=fs.getfilename(\"c:\\test\\test.htm\")
response.write(p)
set fs=nothing
%>

输出:

test.htm

<%
dim fs,p
set fs=Server.Create (\" ing.FileSystem \")
p=fs.getfilename(\"c:\\test\\\")
response.write(p)
set fs=nothing
%>

输出:

test

GetFolder方法

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

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

二、例子

<%
dim fs,f
set fs=Server.Create (\" ing.FileSystem \")
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方法返回指定路径中最后部分的父文件夹的名字。

一、语法

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

二、例子

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

输出:

c:\\winnt\\cursors

GetSpecialFolder方法

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

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

二、例子

<%
dim fs,p
set fs=Server.Create (\" ing.FileSystem \")
set p=fs.GetSpecialFolder(1)
Response.Write(p)
set p=nothing
set fs=nothing
%>

输出:

C:\\WINNT\\system32

GetTempName方法

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

一、语法

FileSystem .GetTempName

二、例子

<%
dim fs,tfolder,tname, tfile
Set fs=Server.Create (\" ing.FileSystem \") 
Set tfolder=fs.GetSpecialFolder(2)
tname=fs.GetTempName
Set tfile=tfolder.CreateTextFile(tname) 
Response.write (tfile)
%>

输出:

trb2007.tmp

MoveFile方法

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

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
fs.MoveFile \"c:\\web\\*.gif\",\"c:\\images\\\"
set fs=nothing
%>

MoveFolder方法

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

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

二、例子

<%
dim fs
set fs=Server.Create (\" ing.FileSystem \")
fs.MoveFolder \"c:\\test\\web\\\",\"c:\\windows\\\"
set fs=nothing
%>

OpenTextFile方法

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

一、语法
FileSystem .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.Create (\" ing.FileSystem \")
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

属性 De ion
描述 
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 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
方法 De ion
描述 
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 to access the file
打开指定文件返回一个TextStream对象

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

收藏 打印