ASP中常用的22個FSO文件操作函數整理
在ASP中,FSO的意思是File System Object,即文件系統對象。我們將要操縱的計算機文件系統,在這里是指位于web服務器之上。所以,確認你對此擁有合適的權限。理想情況下,你可以在自己的機器上建立一個web服務器,這樣就能方便地進行測試。如果運行于Windows平臺,請試一試微軟公司的Web服務器iis。
FSO 模型對象
Drive Object:驅動器對象 供存取磁盤或者網絡驅動器
FileSystemObject Object:文件系統對象 供存取計算機的文件系統
Folder Object:文件夾對象 供存取文件夾的所有屬性
TextStream Object:文本流對象 供存取文件內容
你可以使用上面的對象做計算機上的任何事情,也包括破壞活動 ;-( 所以,請小心使用FSO。在web環境中,存儲信息是非常重要的,比如用戶信息,日志文件,等等。FSO提供了一個強大且簡單的方法高效率地保存數據。FSO由微軟公司提供支持,對于非Windows系統,大概不能再使用ASP。
1.文件操作,取文件大小
Function GetFileSize(FileName) "http://功能:取文件大小 "http://形參:文件名 "http://返回值:成功為文件大小,失敗為-1 "http:// Dim f If ReportFileStatus(FileName) = 1 Then Set f = fso.Getfile(FileName) GetFileSize = f.Size Else GetFileSize = -1 End if End Function
2.使用FSO刪除指定文件
Function deleteAFile(filespec) "http://功能:文件刪除 "http://形參:文件名 "http://返回值:成功為1,失敗為-1 "http:// If ReportFileStatus(filespec) = 1 Then fso.deleteFile(filespec) deleteAFile = 1 Else deleteAFile = -1 End if End Function
3.FSO顯示指定目錄下的所有文件
Function ShowFileList(folderspec) "http://功能:目錄存在時顯示此目錄下的所有文件 "http://形參:目錄名 "http://返回值:成功為文件列表,失敗為-1 "http:// Dim f, f1, fc, s If ReportFolderStatus(folderspec) = 1 Then Set f = fso.GetFolder(folderspec) Set fc = f.Files For Each f1 in fc s = s & f1.name s = s & "|" Next ShowFileList = s Else ShowFileList = -1 End if End Function
4.使用fso復制指定文件
Function CopyAFile(SourceFile,DestinationFile) "http://功能:源文件存在時,才能對文件進行復制,目的文件無影響 "http://形參:源文件,目的文件 "http://返回值:成功為1,失敗為-1 "http:// Dim MyFile If ReportFileStatus(SourceFile) = 1 Then Set MyFile = fso.GetFile(SourceFile) MyFile.Copy (DestinationFile) CopyAFile = 1 Else CopyAFile = -1 End if End Function
5.源文件存在時目的文件不存在時才能對文件進行移動
"Response.Write MoveAFile("f:\123\4561.exe","f:\123\4562.txt") Function MoveAFile(SourceFile,DestinationFile) "http://形參:源文件,目的文件 "http://返回值:成功為1,失敗為-1 "http:// If ReportFileStatus(SourceFile)=1 And ReportFileStatus(DestinationFileORPath) =-1 Then fso.MoveFile SourceFile,DestinationFileORPath MoveAFile = 1 Else MoveAFile = -1 End if End Function
6.FSO判斷指定文件是否存在?
Function ReportFileStatus(FileName) "http://功能:判斷文件是否存在 "http://形參:文件名 "http://返回值:成功為1,失敗為-1 "http:// Dim msg msg = -1 If (fso.FileExists(FileName)) Then msg = 1 Else msg = -1 End If ReportFileStatus = msg End Function
7.FSO讀取文件創建日期
Function ShowDatecreated(filespec) "http://功能:文件創建日期 "http://形參:文件名 "http://返回值:成功:文件創建日期,失敗:-1 "http:// Dim f If ReportFileStatus(filespec) = 1 Then Set f = fso.GetFile(filespec) ShowDatecreated = f.Datecreated Else ShowDatecreated = -1 End if End Function
8.FSO顯示文件讀寫權限屬性
Function GetAttributes(FileName) "http://功能:顯示文件屬性 "http://形參:文件名 "http://返回值:成功:文件屬性,失敗:-1 "http:// Dim f,Str If ReportFileStatus(FileName) = 1 Then Set f = fso.GetFile(FileName) select Case f.attributes Case 0 Str="普通文件。沒有設置任何屬性。 " Case 1 Str="只讀文件。可讀寫。 " Case 2 Str="隱藏文件。可讀寫。 " Case 4 Str="系統文件。可讀寫。 " Case 16 Str="文件夾或目錄。只讀。 " Case 32 Str="上次備份后已更改的文件。可讀寫。 " Case 1024 Str="鏈接或快捷方式。只讀。 " Case 2048 Str=" 壓縮文件。只讀。" End select GetAttributes = Str Else GetAttributes = -1 End if End Function
9.FSO顯示指定文件最后一次訪問/最后一次修改時間
"Response.Write ShowFileAccessInfo("文件路徑") Function ShowFileAccessInfo(FileName,InfoType) "http://功能:顯示文件創建時信息 "http://形參:文件名,信息類別 "http:// 1 -----創建時間 "http:// 2 -----上次訪問時間 "http:// 3 -----上次修改時間 "http:// 4 -----文件路徑 "http:// 5 -----文件名稱 "http:// 6 -----文件類型 "http:// 7 -----文件大小 "http:// 8 -----父目錄 "http:// 9 -----根目錄 "http://返回值:成功為文件創建時信息,失敗:-1 "http:// Dim f, s If ReportFileStatus(FileName) = 1 then Set f = fso.GetFile(FileName) select Case InfoType Case 1 s = f.Datecreated "http:// 1 -----創建時間 Case 2 s = f.DateLastAccessed "http:// 2 -----上次訪問時間 Case 3 s = f.DateLastModified "http:// 3 -----上次修改時間 Case 4 s = f.Path "http:// 4-----文件路徑 Case 5 s = f.Name "http:// 5 -----文件名稱 Case 6 s = f.Type "http:// 6-----文件類型 Case 7 s = f.Size "http:// 7-----文件大小 Case 8 s = f.ParentFolder "http:// 8 -----父目錄 Case 9 s = f.RootFolder "http:// 8 -----根目錄 End select ShowFileAccessInfo = s ELse ShowFileAccessInfo = -1 End if End Function
10.FSO寫指定內容到文本文件
Function WriteTxtFile(FileName,TextStr,WriteORAppendType) Const ForReading = 1, ForWriting = 2 , ForAppending = 8 Dim f, m select Case WriteORAppendType Case 1: "文件進行寫操作 Set f = fso.OpenTextFile(FileName, ForWriting, True) f.Write TextStr f.Close If ReportFileStatus(FileName) = 1 then WriteTxtFile = 1 Else WriteTxtFile = -1 End if Case 2: "文件末尾進行寫操作 If ReportFileStatus(FileName) = 1 then Set f = fso.OpenTextFile(FileName, ForAppending) f.Write TextStr f.Close WriteTxtFile = 1 Else WriteTxtFile = -1 End if End select End Function
11.利用FSO讀取文本文件內容
Function ReadTxtFile(FileName) Const ForReading = 1, ForWriting = 2 Dim f, m If ReportFileStatus(FileName) = 1 then Set f = fso.OpenTextFile(FileName, ForReading) m = f.ReadLine "m = f.ReadAll "f.SkipLine ReadTxtFile = m f.Close Else ReadTxtFile = -1 End if End Function
12.FSO返回文件夾目錄空間大小
Function GetFolderSize(FolderName) "http://功能:取目錄大小 "http://形參:目錄名 "http://返回值:成功為目錄大小,失敗為-1 "http:// Dim f If ReportFolderStatus(FolderName) = 1 Then Set f = fso.GetFolder(FolderName) GetFolderSize = f.Size Else GetFolderSize = -1 End if End Function
13.使用FSO創建文件夾
Function createFolderDemo(FolderName) "http://功能:創建的文件夾 "http://形參:目錄名 "http://返回值:成功為1,失敗為-1 "http:// Dim f If ReportFolderStatus(Folderspec) = 1 Then createFolderDemo = -1 Else Set f = fso.createFolder(FolderName) createFolderDemo = 1 End if End Function
14.FSO刪除指定文件夾目錄
Function deleteAFolder(Folderspec) "http://功能:目錄刪除 "http://形參:目錄名 "http://返回值:成功為1,失敗為-1 "http:// Response.write Folderspec If ReportFolderStatus(Folderspec) = 1 Then fso.deleteFolder (Folderspec) deleteAFolder = 1 Else deleteAFolder = -1 End if End Function
15.FSO顯示指定目錄的文件夾目錄列表
Function ShowFolderList(folderspec) "http://功能:目錄存在時顯示此目錄下的所有子目錄 "http://形參:目錄名 "http://返回值:成功為子目錄列表,失敗為-1 "http:// Dim f, f1, fc, s If ReportFolderStatus(folderspec) = 1 Then Set f = fso.GetFolder(folderspec) Set fc = f.SubFolders For Each f1 in fc s = s & f1.name s = s & "|" Next ShowFolderList = s Else ShowFolderList = -1 End if End Function
16.FSO復制指定文件夾目錄
Function CopyAFolder(SourceFolder,DestinationFolder) "http://功能:源目錄存在時,才能對目錄進行復制,目的目錄無影響 "http://形參:源目錄,目的目錄 "http://返回值:成功為1,失敗為-1 "http:// Dim MyFolder If ReportFolderStatus(SourceFolder) = 1 and ReportFolderStatus(DestinationFolder) = -1 Then Set MyFolder = fso.GetFolder(SourceFolder) fso.CopyFolder SourceFolder,DestinationFolder CopyAFolder = 1 Else CopyAFolder = -1 End if End Function
17.移動指定文件夾目錄
Function MoveAFolder(SourcePath,DestinationPath) "http://功能:源目錄存在時目的目錄不存在時才能對目錄進行移動 "http://形參:源目錄,目的目錄 "http://返回值:成功為1,失敗為-1 "http:// If ReportFolderStatus(SourcePath)=1 And ReportFolderStatus(DestinationPath)=0 Then fso.MoveFolder SourcePath, DestinationPath MoveAFolder = 1 Else MoveAFolder = -1 End if End Function
18.判斷某目錄是否存在
"Response.Write ReportFolderStatus("G:\soft\delphi\my_pro\") Function ReportFolderStatus(fldr) "http://功能:判斷目錄是否存在 "http://形參:目錄 "http://返回值:成功為1,失敗為-1 "http:// Dim msg msg = -1 If (fso.FolderExists(fldr)) Then msg = 1 Else msg = -1 End If ReportFolderStatus = msg End Function
19.顯示目錄創建時信息
Function ShowFolderAccessInfo(FolderName,InfoType) "http://功能:顯示目錄創建時信息 "http://形參:目錄名,信息類別 "http:// 1 -----創建時間 "http:// 2 -----上次訪問時間 "http:// 3 -----上次修改時間 "http:// 4 -----目錄路徑 "http:// 5 -----目錄名稱 "http:// 6 -----目錄類型 "http:// 7 -----目錄大小 "http:// 8 -----父目錄 "http:// 9 -----根目錄 "http://返回值:成功為目錄創建時信息,失敗:-1 "http:// Dim f, s If ReportFolderStatus(FolderName) = 1 then Set f = fso.GetFolder(FolderName) select Case InfoType Case 1 s = f.Datecreated "http:// 1 -----創建時間 Case 2 s = f.DateLastAccessed "http:// 2 -----上次訪問 時間 Case 3 s = f.DateLastModified "http:// 3 -----上次修改時間 Case 4 s = f.Path "http:// 4-----文件路徑 Case 5 s = f.Name "http:// 5-----文件名稱 Case 6 s = f.Type "http:// 6-----文件類型 Case 7 s = f.Size "http:// 7-----文件大小 Case 8 s = f.ParentFolder "http:// 8 -----父目錄 Case 9 s = f.RootFolder "http:// 9 -----根目錄 End select ShowFolderAccessInfo = s ELse ShowFolderAccessInfo = -1 End if End Function
20.返回文件夾嵌套數
Function DisplayLevelDepth(pathspec) Dim f, n ,Path Set f = fso.GetFolder(pathspec) If f.IsRootFolder Then DisplayLevelDepth ="指定的文件夾是根文件夾。"&RootFolder Else Do Until f.IsRootFolder Path = Path & f.Name &" " Set f = f.ParentFolder n = n + 1 Loop DisplayLevelDepth ="指定的文件夾是嵌套級為 " & n & "的文件夾。 "&Path End If End Function
21.判斷指定磁盤驅動器是否存在?
"Response.Write ReportDriveStatus("C:\") Function ReportDriveStatus(drv) "http://功能:判斷磁盤是否存在 "http://形參:磁盤 "http://返回值:成功為1,失敗為-1 "http:// Dim msg msg = -1 If fso.DriveExists(drv) Then msg = 1 Else msg = -1 End If ReportDriveStatus = msg End Function
22.FSO返回指定磁盤可用的類型包括 FAT、NTFS 和 CDFS。
"Response.Write ShowFileSystemType("C:\") Function ShowFileSystemType(drvspec) "http://功能:磁盤類型 "http://形參:磁盤名 "http://返回值:成功為類型:FAT、NTFS 和 CDFS,失敗:-1 "http:// Dim d If ReportDriveStatus(drvspec) = 1 Then Set d = fso. GetDrive(drvspec) ShowFileSystemType = d.FileSystem ELse ShowFileSystemType = -1 End if End Function
相關文章: