|
|
|
|
你现在的位置:您现在的位置是: 中国ASP>>ASP教程>>fso组件 |
|
| fso:file 文件对象 |
|
File对象 File对象提供了对文件的属性的访问,通过它的方法能够对文件进行操作。每个Folder对象提供了一个Files集合,包含文件夹中文件对应的File对象。还可以直接地从FileSystemObject对象中通过使用GetFile方法得到一个File对象引用。 (1) File对象的属性 File对象有一系列的属性,类似于Folder对象的属性,:
Attributes 返回文件的属性。可以是下列值中的一个或其组合:Normal(0)、ReadOnly(1)、Hidden(2)、System(4)、Volume(名称)(9)、Directory(文件夹)(16)、Archive(32)、Alias(64)和Compressed(128)
DateCreated 返回该文件夹的创建日期和时间
DateLastAccessed 返回最后一次访问该文件的日期和时间
DateLastModified 返回最后一次修改该文件的日期和时间
Drive 返回该文件所在的驱动器的Drive对象
Name 设定或返回文件的名字
ParentFolder 返回该文件的父文件夹的Folder对象
Path 返回文件的绝对路径,可使用长文件名
ShortName 返回DOS风格的8.3形式的文件名
ShortPath 返回DOS风格的8.3形式的文件绝对路径
Size 返回该文件的大小(字节)
Type 如果可能,返回一个文件类型的说明字符串(例如:“Text Document”表示.txt文件)
(2) File对象的方法 同样类似于Folder对象,File对象的方法允许复制、删除以及移动文件。它也有一个使用文本流打开文件的方法。 File对象的方法及说明 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
OpenAsTextStream (iomode,format) 打开指定文件并且返回一个TextStream对象,用于文件的读、写或追加。iomode参数指定了要求的访问类型,允许值是ForReading(1) (缺省值)、ForWrite(2)、ForAppending(8)。format参数说明了读、写文件的数据格式。允许值是TristateFalse(0)(缺省),说明用ASCII数据格式;TristateTrue(-1)说明用Unicode数据格式;TristateUseDefault(-2)说明使用系统缺省格式
因此给定一个File对象后,可以使用ParentFolder属性得到包含该文件的Folder对象的引用,用来在文件系统中导航。甚至可以用Drive属性获得相应的Drive对象的引用,并得到各种Folder对象以及所包含的File对象。 另外,给定一个Folder对象以及对应的Files集合后,可以通过遍历该集合检查这一文件夹中的每个文件。还可以使用File对象的各种方法以一定方式处理该文件,如复制、移动或删除。下面的代码给出了C驱动器的第一个文件夹的文件列表: "" 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 注意,不能使用数字索引来定位Folders或Files集合里的条目,因此必须使用For Each … Next语句遍历该集合直到最初的条目,然后使用该条目的Name属性。也不得不使用嵌套的圆括号强迫其作为值(字符串)传送给该Folders集合的Item方法。 用下面的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>""); } 两个程序的结果是相同的.
|
|
|