ASP 3.0高級編程(二十一)
發表于:2007-06-30來源:作者:點擊數:
標簽:
1. Folder對象 Driver對象的RootFolder屬性返回一個Folder對象,通過該對象可訪問這個驅動器內的所有的內容??梢允褂眠@個Folder對象的屬性和方法遍歷驅動器上的目錄,并得到該文件夾和其他文件夾的屬性。 (1) Folder對象的屬性 Folder對象提供一組屬性,可
1. Folder對象
Driver對象的RootFolder屬性返回一個Folder對象,通過該對象可訪問這個驅動器內的所有的內容??梢允褂眠@個Folder對象的屬性和方法遍歷驅動器上的目錄,并得到該文件夾和其他文件夾的屬性。
(1) Folder對象的屬性
Folder對象提供一組屬性,可用這些屬性得到關于當前文件夾的更多信息,也可以改變該文件夾的名稱。其屬性及說明如表5-9所示:
表5-9 Folder 對象的屬性及說明
屬 性
說 明
Attributes
返回文件夾的屬性??梢允窍铝兄抵械囊粋€或其組合:Normal(0)、ReadOnly(1)、Hidden(2)、System(4)、Volume(名稱)(8)、Directory(文件夾)(16)、Archive(32)、Alias(64)和Compressed(128)。例如,一個隱藏的只讀文件,Attributes的值為3
DateCreated
返回該文件夾的創建日期和時間
DateLastA
clearcase/" target="_blank" >ccessed
返回最后一次訪問該文件夾的日期和時間
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所示:
表5-10 Folder對象的方法及說明
方 法
說 明
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>‘’);
}
該VBScript程序在
服務器上運行時的結果如圖5-12所示。該頁面為folderscollection_
vb.asp,來自本書提供的示例文件。
圖5-12 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>‘’);
該VBScript程序在服務器上運行時的結果如圖5-13所示。該頁面名為specialfolder_vb.asp,來自本書提供的示例文件。
圖5-13 GetSpecialFolder方法的使用結果
2. File對象
File對象提供了對文件的屬性的訪問,通過它的方法能夠對文件進行操作。每個Folder對象提供了一個Files集合,包含文件夾中文件對應的File對象。還可以直接地從FileSystemObject對象中通過使用GetFile方法得到一個File對象引用。
(1) File對象的屬性
File對象有一系列的屬性,類似于Folder對象的屬性,如表5-11所示:
表5-11 File對象的屬性及說明
屬 性
說 明
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對象的方法及說明如表5-12所示:
表5-12 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>‘’);
}
兩個程序的結果是相同的,如圖5-14所示。該頁面為filescollection_vb.asp,來自本書提供的示例文件。
圖5-14 File集合的內容
5.5 Scripting.TextStream對象
FileSystemObject、Folder和File對象的一些方法都與通過TextStream對象創建、讀取或寫入文件有關。
雖然TextStream對象定義為FileSystemObject對象的一個獨立的附屬對象,但我們不得不使用FileSystemObject對象或其附屬對象來創建一個TextStream對象并訪問磁盤文件的內容。
原文轉自:http://www.kjueaiud.com