dd

ASP FileSystemObject 对象


FileSystemObject 对象用于访问服务器上的文件系统。

FileSystemObject 对象

FileSystemObject 对象用于访问服务器上的文件系统。

此对象可对文件、文件夹和目录路径进行操作。也可通过此对象获取文件系统的信息。

下面的代码会创建一个文本文件 (c:\test.txt),然后向这个文件写一些文本:

<%
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返回本地计算机上所有驱动器对象的集合。

方法

方法描述
BuildPath将一个名称追加到已有的路径后。
CopyFile从一个位置向另一个位置拷贝一个或多个文件。
CopyFolder从一个位置向另一个位置拷贝一个或多个文件夹。
CreateFolder创建新文件夹。
CreateTextFile创建文本文件,并返回一个可以读取或者写入文件的 TextStream 对象。
DeleteFile删除一个或者多个指定的文件。
DeleteFolder删除一个或者多个指定的文件夹。
DriveExists检查指定的驱动器是否存在。
FileExists检查指定的文件是否存在。
FolderExists检查指定的文件夹是否存在。
GetAbsolutePathName针对指定的路径返回从驱动器根部起始的完整路径。
GetBaseName返回指定文件或者文件夹的基名称。
GetDrive返回指定路径中所对应的驱动器的 Drive 对象。
GetDriveName返回指定的路径的驱动器名称。
GetExtensionName返回在指定的路径中最后一个成分的文件扩展名。
GetFile返回一个针对指定路径的 File 对象。
GetFileName返回在指定的路径中最后一个成分的文件名或者文件夹名。
GetFolder返回一个针对指定路径的 Folder 对象。
GetParentFolderName返回在指定的路径中最后一个成分的父文件夹名称。
GetSpecialFolder返回某些 Windows 的特殊文件夹的路径。
GetTempName返回一个随机生成的文件或文件夹。
MoveFile从一个位置向另一个位置移动一个或多个文件。
MoveFolder从一个位置向另一个位置移动一个或多个文件夹。
OpenTextFile打开文件,并返回一个用于访问此文件的 TextStream 对象。

在线实例

指定的文件存在吗?
本例演示如何检查某个文件是否存在。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

If (fs.FileExists("c:\winnt\cursors\3dgarro.cur"))=true Then
      Response.Write("File c:\winnt\cursors\3dgarro.cur exists.")
Else
      Response.Write("File c:\winnt\cursors\3dgarro.cur does not exist.")
End If

set fs=nothing
%>

</body>
</html>

指定的文件夹存在吗?
本例演示如何检查某个文件夹是否存在。

<!DOCTYPE html>
<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

If fs.FolderExists("c:\temp") = true Then
      Response.Write("Folder c:\temp exists.")
Else
      Response.Write("Folder c:\temp does not exist.")
End If

set fs=nothing
%>

</body>
</html>

指定的驱动器存在吗?
本例演示如何检查某个驱动器是否存在。

<!DOCTYPE html>
<html>
<body>
<%
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

Response.write("<br>")

if fs.driveexists("g:") = true then
      Response.Write("Drive g: exists.")
Else
      Response.Write("Drive g: does not exist.")
End If

set fs=nothing
%>

</body>
</html>

取得某个指定驱动器的名称
本例演示如何获取某个指定的驱动器的名称。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetDriveName("c:\winnt\cursors\3dgarro.cur")

Response.Write("The drive name is: " & p)

set fs=nothing
%>

</body>
</html>

取得某个指定路径的父文件夹的名称
本例演示如何获取某个指定的路径的父文件夹的名称。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur")

Response.Write("The parent folder name of c:\winnt\cursors\3dgarro.cur is: " & p)

set fs=nothing
%>

</body>
</html>

取得文件名
本例演示如何获取指定的路径中的最后一个成分的文件名。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write("The file name of the last component is: ")
Response.Write(fs.GetFileName("c:\winnt\cursors\3dgarro.cur"))
set fs=nothing
%>


</body>
</html>

取得文件扩展名
本例演示如何获取指定的路径中的最后一个成分的文件扩展名。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write("The file extension of the file 3dgarro is: ")
Response.Write(fs.GetExtensionName("c:\winnt\cursors\3dgarro.cur"))

set fs=nothing
%>

</body>
</html>

取得文件或文件夹的基名称
本例演示如何获取指定的路径中文件或者文件夹的基名称。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write(fs.GetBaseName("c:\winnt\cursors\3dgarro.cur"))
Response.Write("<br>")
Response.Write(fs.GetBaseName("c:\winnt\cursors\"))
Response.Write("<br>")
Response.Write(fs.GetBaseName("c:\winnt\"))

set fs=nothing
%>

</body>
</html>