1. 程式人生 > 程式設計 >ASP中常用的22個FSO檔案操作函式整理

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)
'//功能:取檔案大小
'//形參:檔名
'//返回值:成功為檔案大小,失敗為-1
'//
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)
'//功能:檔案刪除
'//形參:檔名
'//返回值:成功為1,失敗為-1
'//
If ReportFileStatus(filespec) = 1 Then
fso.deleteFile(filespec)
deleteAFile = 1
Else
deleteAFile = -1
End if
End Function 

3.FSO顯示指定目錄下的所有檔案

Function ShowFileList(folderspec)
'//功能:目錄存在時顯示此目錄下的所有檔案
'//形參:目錄名
'//返回值:成功為檔案列表,失敗為-1
'//
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)
'//功能:原始檔存在時,才能對檔案進行復制,目的檔案無影響
'//形參:原始檔,目的檔案
'//返回值:成功為1,失敗為-1
'//
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)
'//形參:原始檔,目的檔案
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:判斷檔案是否存在
'//形參:檔名
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:檔案建立日期
'//形參:檔名
'//返回值:成功:檔案建立日期,失敗:-1
'//
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)
'//功能:顯示檔案屬性
'//形參:檔名
'//返回值:成功:檔案屬性,失敗:-1
'//
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)
'//功能:顯示檔案建立時資訊
'//形參:檔名,資訊類別
'// 1 -----建立時間
'// 2 -----上次訪問時間
'// 3 -----上次修改時間
'// 4 -----檔案路徑
'// 5 -----檔名稱
'// 6 -----檔案型別
'// 7 -----檔案大小
'// 8 -----父目錄
'// 9 -----根目錄
'//返回值:成功為檔案建立時資訊,失敗:-1
'//
Dim f,s
If ReportFileStatus(FileName) = 1 then
Set f = fso.GetFile(FileName)
select Case InfoType
Case 1 s = f.Datecreated '// 1 -----建立時間
Case 2 s = f.DateLastAccessed '// 2 -----上次訪問時間
Case 3 s = f.DateLastModified '// 3 -----上次修改時間
Case 4 s = f.Path '// 4-----檔案路徑
Case 5 s = f.Name '// 5 -----檔名稱
Case 6 s = f.Type '// 6-----檔案型別
Case 7 s = f.Size '// 7-----檔案大小
Case 8 s = f.ParentFolder '// 8 -----父目錄
Case 9 s = f.RootFolder '// 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)
'//功能:取目錄大小
'//形參:目錄名
'//返回值:成功為目錄大小,失敗為-1
'//
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)
'//功能:建立的資料夾
'//形參:目錄名
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:目錄刪除
'//形參:目錄名
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:目錄存在時顯示此目錄下的所有子目錄
'//形參:目錄名
'//返回值:成功為子目錄列表,失敗為-1
'//
Dim f,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)
'//功能:源目錄存在時,才能對目錄進行復制,目的目錄無影響
'//形參:源目錄,目的目錄
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:源目錄存在時目的目錄不存在時才能對目錄進行移動
'//形參:源目錄,目的目錄
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:判斷目錄是否存在
'//形參:目錄
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:顯示目錄建立時資訊
'//形參:目錄名,資訊類別
'// 1 -----建立時間
'// 2 -----上次訪問時間
'// 3 -----上次修改時間
'// 4 -----目錄路徑
'// 5 -----目錄名稱
'// 6 -----目錄型別
'// 7 -----目錄大小
'// 8 -----父目錄
'// 9 -----根目錄
'//返回值:成功為目錄建立時資訊,失敗:-1
'//
Dim f,s
If ReportFolderStatus(FolderName) = 1 then
Set f = fso.GetFolder(FolderName)
select Case InfoType
Case 1 s = f.Datecreated '// 1 -----建立時間
Case 2 s = f.DateLastAccessed '// 2 -----上次訪問
時間
Case 3 s = f.DateLastModified '// 3 -----上次修改時間
Case 4 s = f.Path '// 4-----檔案路徑
Case 5 s = f.Name '// 5-----檔名稱
Case 6 s = f.Type '// 6-----檔案型別
Case 7 s = f.Size '// 7-----檔案大小
Case 8 s = f.ParentFolder '// 8 -----父目錄
Case 9 s = f.RootFolder '// 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)
'//功能:判斷磁碟是否存在
'//形參:磁碟
'//返回值:成功為1,失敗為-1
'//
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)
'//功能:磁碟型別
'//形參:磁碟名
'//返回值:成功為型別:FAT、NTFS 和 CDFS,失敗:-1
'//
Dim d
If ReportDriveStatus(drvspec) = 1 Then
Set d = fso. GetDrive(drvspec)
ShowFileSystemType = d.FileSystem
ELse
ShowFileSystemType = -1
End if
End Function