<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<% ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ FileSystemObject 示例代碼 ‘Copyright 2007 獨(dú)孤翼 QQ:88056598 保留所有權(quán)利,。 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Option Explicit
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ 對(duì)于代碼質(zhì)量: ‘ 1) 下面的代碼有許多字符串操作,,用"&"運(yùn)算符來(lái)把短字符串連接在一起,。由于 ‘ 字符串連接是費(fèi)時(shí)的,,所以這是一種低效率的寫(xiě)代碼方法,。無(wú)論如何,,它是 ‘ 一種非常好維護(hù)的寫(xiě)代碼方法,,并且在這兒使用了這種方法,,因?yàn)樵摮绦驁?zhí)行 ‘ 大量的磁盤(pán)操作,而磁盤(pán)操作比連接字符串所需的內(nèi)存操作要慢得多,。 ‘ 記住這是示范代碼,,而不是產(chǎn)品代碼。 ‘ ‘ 2) 使用了 "Option Explicit",,因?yàn)樵L(fǎng)問(wèn)聲明過(guò)的變量,,比訪(fǎng)問(wèn)未聲明的變量要 ‘ 稍微快一些。它還能阻止在代碼中發(fā)生錯(cuò)誤,,例如,,把 DriveTypeCDROM 誤拼 ‘ 成了 DriveTypeCDORM 。 ‘ ‘ 3) 為了使代碼更可讀,,該代碼中沒(méi)有錯(cuò)誤處理,。雖然采取了防范措施,來(lái)保證代碼 ‘ 在普通情況下沒(méi)有錯(cuò)誤,,但文件系統(tǒng)是不可預(yù)知的,。在產(chǎn)品代碼中,使用 ‘ On Error Resume Next 和 Err 對(duì)象來(lái)捕獲可能發(fā)生的錯(cuò)誤,。 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Dim TabStop Dim NewLine ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ 由 File.Attributes 返回的常數(shù) ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Const FileAttrNormal = 0 Const FileAttrReadOnly = 1 Const FileAttrHidden = 2 Const FileAttrSystem = 4 Const FileAttrVolume = 8 Const FileAttrDirectory = 16 Const FileAttrArchive = 32 Const FileAttrAlias = 64 Const FileAttrCompressed = 128 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ 用來(lái)打開(kāi)文件的常數(shù) ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Const OpenFileForReading = 1 Const OpenFileForWriting = 2 Const OpenFileForAppending = 8 ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ ShowFileAttr ‘ 目的: ‘ 生成一個(gè)字符串,來(lái)描述文件或文件夾的屬性,。 ‘ 示范下面的內(nèi)容 ‘ - File.Attributes ‘ - Folder.Attributes ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Function ShowFileAttr(File) ‘ File 可以是文件或文件夾
Dim S
Dim Attr Attr = File.Attributes If Attr = 0 Then
ShowFileAttr = "Normal" Exit Function End If If Attr And FileAttrDirectory Then S = S & "Directory "
If Attr And FileAttrReadOnly Then S = S & "Read-Only " If Attr And FileAttrHidden Then S = S & "Hidden " If Attr And FileAttrSystem Then S = S & "System " If Attr And FileAttrVolume Then S = S & "Volume " If Attr And FileAttrArchive Then S = S & "Archive " If Attr And FileAttrAlias Then S = S & "Alias " If Attr And FileAttrCompressed Then S = S & "Compressed " ShowFileAttr = S
End Function
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ ‘ GenerateFileInformation ‘ 目的: ‘ 生成一個(gè)字符串,,來(lái)描述文件的當(dāng)前狀態(tài)。 ‘ 示范下面的內(nèi)容 ‘ - File.Path ‘ - File.Name ‘ - File.Type ‘ - File.DateCreated ‘ - File.DateLastAccessed ‘ - File.DateLastModified ‘ - File.Size ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Function GenerateFileInformation(File)
Dim S
S = NewLine & "Path:" & TabStop & File.Path
S = S & NewLine & "Name:" & TabStop & File.Name S = S & NewLine & "Type:" & TabStop & File.Type S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File) S = S & NewLine & "Created:" & TabStop & File.DateCreated S = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessed S = S & NewLine & "Modified:" & TabStop & File.DateLastModified S = S & NewLine & "Size" & TabStop & File.Size & NewLine GenerateFileInformation = S
End Function
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ GenerateFolderInformation ‘ 目的: ‘ 生成一個(gè)字符串,,來(lái)描述文件夾的當(dāng)前狀態(tài),。 ‘ 示范下面的內(nèi)容 ‘ - Folder.Path ‘ - Folder.Name ‘ - Folder.DateCreated ‘ - Folder.DateLastAccessed ‘ - Folder.DateLastModified ‘ - Folder.Size ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Function GenerateFolderInformation(Folder)
Dim S
S = "Path:" & TabStop & Folder.Path
S = S & NewLine & "Name:" & TabStop & Folder.Name S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(Folder) S = S & NewLine & "Created:" & TabStop & Folder.DateCreated S = S & NewLine & "Accessed:" & TabStop & Folder.DateLastAccessed S = S & NewLine & "Modified:" & TabStop & Folder.DateLastModified S = S & NewLine & "Size:" & TabStop & Folder.Size & NewLine GenerateFolderInformation = S
End Function
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ GenerateAllFolderInformation ‘ 目的: ‘ 生成一個(gè)字符串,來(lái)描述一個(gè)文件夾和所有文件及子文件夾的當(dāng)前狀態(tài),。 ‘ 示范下面的內(nèi)容 ‘ - Folder.Path ‘ - Folder.SubFolders ‘ - Folders.Count ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ Function GenerateAllFolderInformation(Folder)
Dim S
Dim SubFolders Dim SubFolder Dim Files Dim File S = "Folder:" & TabStop & Folder.Path & NewLine & NewLine
Set Files = Folder.Files If 1 = Files.Count Then
S = S & "There is 1 file" & NewLine Else S = S & "There are " & Files.Count & " files" & NewLine End If If Files.Count <> 0 Then
For Each File In Files S = S & GenerateFileInformation(File) Next End If Set SubFolders = Folder.SubFolders
If 1 = SubFolders.Count Then
S = S & NewLine & "There is 1 sub folder" & NewLine & NewLine Else S = S & NewLine & "There are " & SubFolders.Count & " sub folders" & NewLine & NewLine End If If SubFolders.Count <> 0 Then
For Each SubFolder In SubFolders S = S & GenerateFolderInformation(SubFolder) Next S = S & NewLine For Each SubFolder In SubFolders S = S & GenerateAllFolderInformation(SubFolder) Next End If GenerateAllFolderInformation = S
End Function
Sub Main ‘ 設(shè)立全局變量,。
TabStop = " "‘Chr(9) NewLine = "<br>"‘Chr(10) Dim objFolder Dim objFSO ‘ 建立FSO和文件夾對(duì)象 Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(Server.MapPath(".")) ‘獲取當(dāng)前目錄路徑 Response.Write GenerateFolderInformation(objFolder)&"<br>" Response.Write GenerateAllFolderInformation(objFolder) End Sub %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>文件系統(tǒng)信息表</title> </head> <body>
<% Main %> </body> </html> |
|
來(lái)自: 昵稱(chēng)19250 > 《asp技術(shù)》