ASP 3.0高級編程(二11)
發(fā)表時間:2023-08-08 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]1. Folder對象Driver對象的RootFolder屬性返回一個Folder對象,通過該對象可訪問這個驅(qū)動器內(nèi)的所有的內(nèi)容。可以使用這個Folder對象的屬性和方法遍歷驅(qū)動器上的目錄,并得...
1. Folder對象
Driver對象的RootFolder屬性返回一個Folder對象,通過該對象可訪問這個驅(qū)動器內(nèi)的所有的內(nèi)容。可以使用這個Folder對象的屬性和方法遍歷驅(qū)動器上的目錄,并得到該文件夾和其他文件夾的屬性。
(1) Folder對象的屬性
Folder對象提供一組屬性,可用這些屬性得到關(guān)于當(dāng)前文件夾的更多信息,也可以改變該文件夾的名稱。其屬性及說明如表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
返回該文件夾的創(chuàng)建日期和時間
DateLastAccessed
返回最后一次訪問該文件夾的日期和時間
DateLastModified
返回最后一次修改該文件夾的日期和時間
Drive
返回該文件夾所在的驅(qū)動器的驅(qū)動器字母
Files
返回Folder對象包含的Files集合,表示該文件夾內(nèi)所有的文件
IsRootFolder
返回一個布爾值說明該文件夾是否是當(dāng)前驅(qū)動器的根文件夾
Name
設(shè)定或返回文件夾的名字
ParentFolder
返回該文件夾的父文件夾對應(yīng)的Folder對象
Path
返回文件夾的絕對路徑,使用相應(yīng)的長文件名
ShortName
返回DOS風(fēng)格的8.3形式的文件夾名
ShortPath
返回DOS風(fēng)格的8.3形式的文件夾的絕對路徑
Size
返回包含在該文件夾里所有文件和子文件夾的大小
SubFolers
返回該文件夾內(nèi)包含的所有子文件夾對應(yīng)的Folders集合,包括隱藏文件夾和系統(tǒng)文件夾
Type
如果可能,返回一個文件夾的說明字符串(例如,“Recycle Bin”)
(2) Folder對象的方法
Folder對象提供一組可用于復(fù)制、刪除和移動當(dāng)前文件夾的方法。這些方法的運(yùn)行方式與FileSystemObject對象的CopyFolder、DeleFolder和MoveFolder方法相同,但這些方法不要求source參數(shù),因為源文件就是這個文件夾。這些方法及說明如表5-10所示:
表5-10 Folder對象的方法及說明
方 法
說 明
Copy(destination,overwrite)
將這個文件夾及所有的內(nèi)容復(fù)制到destination指定的文件夾。如果destination的末尾是路徑分隔符(‘\’),那么認(rèn)為destination是放置拷貝文件夾的一個文件夾。否則認(rèn)為destination是要創(chuàng)建的新文件夾的路徑和名字。如果目標(biāo)文件夾已經(jīng)存在且overwrite參數(shù)設(shè)置為False,將產(chǎn)生錯誤,缺省的overwrite參數(shù)是True
Delete(force)
刪除文件夾及里面的所有內(nèi)容。如果可選的force參數(shù)設(shè)置為True,即使文件夾設(shè)置為只讀或含有只讀的文件,也將刪除該文件夾。缺省的force是False
Move(destination)
將文件夾及里面所有的內(nèi)容移動到destination指定的文件夾。如果destination的末尾是路徑分隔符(‘\’),那么認(rèn)為destination是放置移動文件夾的一個文件夾。否則認(rèn)為destination是一個新的文件夾的路徑和名字。如果目標(biāo)文件夾已經(jīng)存在,則出錯
CreateTextFile
(filename,overwrite,unicode)
用指定的文件名在文件夾內(nèi)創(chuàng)建一個新的文本文件,并且返回一個相應(yīng)的TextStream對象。如果可選的overwrite參數(shù)設(shè)置為True,將覆蓋任何已有的同名文件。缺省的overwrite參數(shù)是False。如果可選的unicode參數(shù)設(shè)置為True,文件的內(nèi)容將存儲為unicode文本。缺省的unicode是False
在文件夾之間可以使用當(dāng)前文件夾的ParentFolder屬性,返回到父目錄。當(dāng)?shù)竭_(dá)一個文件夾時,如果IsRootFolder屬性是True,就停下來。離開驅(qū)動器的根目錄,沿目錄樹向下,可遍歷或訪問在Folders集合(由當(dāng)前文件夾的SubFolders屬性返回)內(nèi)的指定文件夾。
下列程序遍歷了驅(qū)動器C根目錄內(nèi)的所有文件夾,并顯示各個文件夾的有關(guān)信息。
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程序在服務(wù)器上運(yùn)行時的結(jié)果如圖5-12所示。該頁面為folderscollection_vb.asp,來自本書提供的示例文件。
圖5-12 Folders集合的內(nèi)容
(3) 使用特殊文件夾
GetSpecialFolder是FileSystemObject對象的方法之一,它返回計算機(jī)上三個“特殊文件夾”對應(yīng)的Folder對象:
· WindowsFolder:%Windows%目錄,缺省為WinNT(或Windows,在非NT/2000計算機(jī)上)目錄。
· SystemFolder:%System%目錄,缺省為WinNT\System32(或Windows\System,在非NT/2000計算機(jī)上)目錄。
· TemporaryFolder:%Temp%目錄,缺省為WinNT\Temp(或Windows\Temp,在非NT/2000計算機(jī)上)目錄。
為得到對特殊文件夾的引用,我們提供相應(yīng)的預(yù)定義常數(shù)作為GetSpecialFolder方法的參數(shù):
' 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程序在服務(wù)器上運(yùn)行時的結(jié)果如圖5-13所示。該頁面名為specialfolder_vb.asp,來自本書提供的示例文件。
圖5-13 GetSpecialFolder方法的使用結(jié)果
2. File對象
File對象提供了對文件的屬性的訪問,通過它的方法能夠?qū)ξ募M(jìn)行操作。每個Folder對象提供了一個Files集合,包含文件夾中文件對應(yīng)的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
返回該文件夾的創(chuàng)建日期和時間
DateLastAccessed
返回最后一次訪問該文件的日期和時間
DateLastModified
返回最后一次修改該文件的日期和時間
Drive
返回該文件所在的驅(qū)動器的Drive對象
Name
設(shè)定或返回文件的名字
ParentFolder
返回該文件的父文件夾的Folder對象
Path
返回文件的絕對路徑,可使用長文件名
ShortName
返回DOS風(fēng)格的8.3形式的文件名
ShortPath
返回DOS風(fēng)格的8.3形式的文件絕對路徑
Size
返回該文件的大小(字節(jié))
Type
如果可能,返回一個文件類型的說明字符串(例如:“Text Document”表示.txt文件)
(2) File對象的方法
同樣類似于Folder對象,F(xiàn)ile對象的方法允許復(fù)制、刪除以及移動文件。它也有一個使用文本流打開文件的方法。File對象的方法及說明如表5-12所示:
表5-12 File對象的方法及說明
方 法
說 明
Copy(destination,overwrite)
將這個文件復(fù)制到destination指定的文件夾。如果destination的末尾是路徑分隔符(‘\’),那么認(rèn)為destination是放置拷貝文件的文件夾。否則認(rèn)為destination是要創(chuàng)建的新文件的路徑和名字。如果目標(biāo)文件已經(jīng)存在且overwrite參數(shù)設(shè)置為False,將產(chǎn)生錯誤,缺省的overwrite參數(shù)是True
Delete(force)
刪除這個文件。如果可選的force參數(shù)設(shè)置為True,即使文件具有只讀屬性也會被刪除。缺省的force是False
Move(destination)
將文件移動到destination指定的文件夾。如果destination的末尾是路徑分隔符(‘\’),那么認(rèn)為destination是一文件夾。否則認(rèn)為destination是一個新的文件的路徑和名字。如果目標(biāo)文件夾已經(jīng)存在,則出錯
CreateTextFile
(filename,overwrite,unicode)
用指定的文件名創(chuàng)建一個新的文本文件,并且返回一個相應(yīng)的TextStream對象。如果可選的overwrite參數(shù)設(shè)置為True,將覆蓋任何已有的同名文件。缺省的overwrite參數(shù)是False。如果可選的unicode參數(shù)設(shè)置為True,文件的內(nèi)容將存儲為unicode文本。缺省的unicode是False
OpenAsTextStream
(iomode,format)
打開指定文件并且返回一個TextStream對象,用于文件的讀、寫或追加。iomode參數(shù)指定了要求的訪問類型,允許值是ForReading(1) (缺省值)、ForWrite(2)、ForAppending(8)。format參數(shù)說明了讀、寫文件的數(shù)據(jù)格式。允許值是TristateFalse(0)(缺省),說明用ASCII數(shù)據(jù)格式;TristateTrue(-1)說明用Unicode數(shù)據(jù)格式;TristateUseDefault(-2)說明使用系統(tǒng)缺省格式
因此給定一個File對象后,可以使用ParentFolder屬性得到包含該文件的Folder對象的引用,用來在文件系統(tǒng)中導(dǎo)航。甚至可以用Drive屬性獲得相應(yīng)的Drive對象的引用,并得到各種Folder對象以及所包含的File對象。
另外,給定一個Folder對象以及對應(yīng)的Files集合后,可以通過遍歷該集合檢查這一文件夾中的每個文件。還可以使用File對象的各種方法以一定方式處理該文件,如復(fù)制、移動或刪除。下面的代碼給出了C驅(qū)動器的第一個文件夾的文件列表:
' 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
注意,不能使用數(shù)字索引來定位Folders或Files集合里的條目,因此必須使用For Each … Next語句遍歷該集合直到最初的條目,然后使用該條目的Name屬性。也不得不使用嵌套的圓括號強(qiáng)迫其作為值(字符串)傳送給該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>');
}
兩個程序的結(jié)果是相同的,如圖5-14所示。該頁面為filescollection_vb.asp,來自本書提供的示例文件。
圖5-14 File集合的內(nèi)容
5.5 Scripting.TextStream對象
FileSystemObject、Folder和File對象的一些方法都與通過TextStream對象創(chuàng)建、讀取或?qū)懭胛募嘘P(guān)。
雖然TextStream對象定義為FileSystemObject對象的一個獨立的附屬對象,但我們不得不使用FileSystemObject對象或其附屬對象來創(chuàng)建一個TextStream對象并訪問磁盤文件的內(nèi)容。