|
|
|
|
你现在的位置:您现在的位置是: 中国ASP>>ASP教程>>fso组件 |
|
| fso:folder 文件系统 |
|
文件系统定位
FileSystemObject的几个方法可用于得到其他对象的引用,因此可以在服务器的文件系统和任何网络驱动器中定位。事实上,在ASP代码里使用的所有对象或组件中,除了ActiveX Data Object组件,FileSystemObject对象很可能是最复杂的对象之一。 这种复杂性是由于对如何访问文件系统的不同部分,要求有极高的灵活性。例如,可以从FileSystemObject向下通过使用各种从属对象定位一个文件。其过程是从Drives集合开始,到一个Drive对象,再到驱动器的根Folder对象,然后到子Folder对象,再到文件夹的Files集合,最后到集合内的File对象。 另外,如果已知要访问的驱动器、文件夹或文件。可以直接对其使用GetDrive、GetFolder、GetSpecialFolder和GetFile方法。
1. Folder对象 Driver对象的RootFolder属性返回一个Folder对象,通过该对象可访问这个驱动器内的所有的内容。可以使用这个Folder对象的属性和方法遍历驱动器上的目录,并得到该文件夹和其他文件夹的属性。 (1) Folder对象的属性 Folder对象提供一组属性,可用这些属性得到关于当前文件夹的更多信息,也可以改变该文件夹的名称。
Attributes 返回文件夹的属性。可以是下列值中的一个或其组合:Normal(0)、ReadOnly(1)、Hidden(2)、System(4)、Volume(名称)(8)、Directory(文件夹)(16)、Archive(32)、Alias(64)和Compressed(128)。例如,一个隐藏的只读文件,Attributes的值为3
DateCreated 返回该文件夹的创建日期和时间
DateLastAccessed 返回最后一次访问该文件夹的日期和时间
DateLastModified 返回最后一次修改该文件夹的日期和时间
Drive 返回该文件夹所在的驱动器的驱动器字母
Files 返回Folder对象包含的Files集合,表示该文件夹内所有的文件
IsRootFolder 返回一个布尔值说明该文件夹是否是当前驱动器的根文件夹
Name 设定或返回文件夹的名字
ParentFolder 返回该文件夹的父文件夹对应的Folder对象
Path 返回文件夹的绝对路径,使用相应的长文件名
ShortName 返回DOS风格的8.3形式的文件夹名
ShortPath 返回DOS风格的8.3形式的文件夹的绝对路径
Size 返回包含在该文件夹里所有文件和子文件夹的大小
SubFolers 返回该文件夹内包含的所有子文件夹对应的Folders集合,包括隐藏文件夹和系统文件夹
Type 如果可能,返回一个文件夹的说明字符串(例如,“Recycle Bin”)
(2) Folder对象的方法 Folder对象提供一组可用于复制、删除和移动当前文件夹的方法。这些方法的运行方式与FileSystemObject对象的CopyFolder、DeleFolder和MoveFolder方法相同,但这些方法不要求source参数,因为源文件就是这个文件夹。这些方法及说明如表5-10所示:
Copy(destination,overwrite) 将这个文件夹及所有的内容复制到destination指定的文件夹。如果destination的末尾是路径分隔符(‘\’),那么认为destination是放置拷贝文件夹的一个文件夹。否则认为destination是要创建的新文件夹的路径和名字。如果目标文件夹已经存在且overwrite参数设置为False,将产生错误,缺省的overwrite参数是True
Delete(force) 删除文件夹及里面的所有内容。如果可选的force参数设置为True,即使文件夹设置为只读或含有只读的文件,也将删除该文件夹。缺省的force是False
Move(destination) 将文件夹及里面所有的内容移动到destination指定的文件夹。如果destination的末尾是路径分隔符(‘\’),那么认为destination是放置移动文件夹的一个文件夹。否则认为destination是一个新的文件夹的路径和名字。如果目标文件夹已经存在,则出错
CreateTextFile (filename,overwrite,unicode) 用指定的文件名在文件夹内创建一个新的文本文件,并且返回一个相应的TextStream对象。如果可选的overwrite参数设置为True,将覆盖任何已有的同名文件。缺省的overwrite参数是False。如果可选的unicode参数设置为True,文件的内容将存储为unicode文本。缺省的unicode是False
在文件夹之间可以使用当前文件夹的ParentFolder属性,返回到父目录。当到达一个文件夹时,如果IsRootFolder属性是True,就停下来。离开驱动器的根目录,沿目录树向下,可遍历或访问在Folders集合(由当前文件夹的SubFolders属性返回)内的指定文件夹。 下列程序遍历了驱动器C根目录内的所有文件夹,并显示各个文件夹的有关信息。 VBscript程序如下: ""In VBscript: "" Create a FileSystemObject instance Set objFSO = Server.CreateObject("scripting.FileSystemObject") "" Get a reference to drive C Set objDriveC = objFSO.GetDrive("C:") "" Get a reference to the root folder Set objRoot = objDriveC.RootFolder "" Get a reference to the SubFolders collection Set objFolders = objRoot.SubFolders "" Get a reference to the first folder in the SubFolders collection For Each objFolder In objFolders Set objFolder1 = objFolders.Item((objFolder.Name)) Exit For Next "" Iterate through all the files in this folder For Each objFile in objFolder1.Files Response.Write "Name: " & objFile.Name & " " Response.Write "ShortName: " & objFile.ShortName & " " Response.Write "Size: " & objFile.Size & " bytes " Response.Write "Type: " & objFile.Type & "<BR>" Response.Write "Path: " & objFile.Path & " " Response.Write "ShortPath: " & objFile.ShortPath & "<BR>" Response.Write "Created: " & objFile.DateCreated & " " Response.Write "LastModified: " & objFile.DateLastModified & "<P>" Next Jscript程序如下: //In Jscript: // Create a FileSystemObject instance var objFSO = Server.CreateObject(""scripting.FileSystemObject""); // Get a reference to drive C var objDriveC = objFSO.GetDrive(""C:""); // Get a reference to the root folder var objRoot = objDriveC.RootFolder; // Get a reference to the first folder in the SubFolders collection var colAllFolders = new Enumerator(objRoot.SubFolders); var objFolder1 = colAllFolders.item(); // Get a reference to the Files collection for this folder var colFiles = new Enumerator(objFolder1.Files);
// Iterate through all the files in this collection for (; !colFiles.atEnd(); colFiles.moveNext()) { objFile = colFiles.item() Response.Write(""Name: "" + objFile.Name + "" ""); Response.Write(""ShortName: "" + objFile.ShortName + "" ""); Response.Write(""Size: "" + objFile.Size + "" bytes ""); Response.Write(""Type: "" + objFile.Type + ""<BR>""); Response.Write(""Path: "" + objFile.Path + "" ""); Response.Write(""ShortPath: "" + objFile.ShortPath + ""<BR>""); Response.Write(""Created: "" + objFile.DateCreated + "" ""); Response.Write(""Accessed: "" + objFile.DateLastAccessed + "" ""); Response.Write(""Modified: "" + objFile.DateLastModified + ""<P>""); }
Folders集合的内容 (3) 使用特殊文件夹 GetSpecialFolder是FileSystemObject对象的方法之一,它返回计算机上三个“特殊文件夹”对应的Folder对象: · WindowsFolder:%Windows%目录,缺省为WinNT(或Windows,在非NT/2000计算机上)目录。 · SystemFolder:%System%目录,缺省为WinNT\System32(或Windows\System,在非NT/2000计算机上)目录。 · TemporaryFolder:%Temp%目录,缺省为WinNT\Temp(或Windows\Temp,在非NT/2000计算机上)目录。 为得到对特殊文件夹的引用,我们提供相应的预定义常数作为GetSpecialFolder方法的参数: "" In VBscript: Set objFSO = Server.CreateObject("scripting.FileSystemObject")
Set objFolder = objFSO.GetSpecialFolder(WindowsFolder) Response.Write "GetSpecialFolder(WindowsFolder) returned:<BR>" Response.Write "Path: " & objFolder.Path & "<BR>" Response.Write "Type: " & objFolder.Type & "<P>"
Set objFolder = objFSO.GetSpecialFolder(SystemFolder) Response.Write "GetSpecialFolder(SystemFolder) returned:<BR>" Response.Write "Path: " & objFolder.Path & "<BR>" Response.Write "Type: " & objFolder.Type & "<P>"
Set objFolder = objFSO.GetSpecialFolder(TemporaryFolder) Response.Write "GetSpecialFolder(TemporaryFolder) returned:<BR>" Response.Write "Path: " & objFolder.Path & "<BR>" Response.Write "Type: " & objFolder.Type & "<P>" 或用Jscript: // In Jscript: var objFSO = Server.CreateObject(""scripting.FileSystemObject"");
var objFolder = objFSO.GetSpecialFolder(WindowsFolder); Response.Write(""GetSpecialFolder(WindowsFolder) returned - ""); Response.Write(""Path: "" + objFolder.Path + "" ""); Response.Write(""Type: "" + objFolder.Type + ""<BR>"");
var objFolder = objFSO.GetSpecialFolder(SystemFolder); Response.Write(""GetSpecialFolder(SystemFolder) returned - ""); Response.Write(""Path: "" + objFolder.Path + "" ""); Response.Write(""Type: "" + objFolder.Type + ""<BR>"");
var objFolder = objFSO.GetSpecialFolder(TemporaryFolder); Response.Write(""GetSpecialFolder(TemporaryFolder) returned - ""); Response.Write(""Path: "" + objFolder.Path + "" ""); Response.Write(""Type: "" + objFolder.Type + ""<BR>"");
|
|
|