在ASP中,F(xiàn)SO的意思是File System Object,即文件系統(tǒng)對(duì)象。我們將要操縱的計(jì)算機(jī)文件系統(tǒng),在這里是指位于web服務(wù)器之上。所以,確認(rèn)你對(duì)此擁有合適的權(quán)限。理想情況下,你可以在自己的機(jī)器上建立一個(gè)web服務(wù)器,這樣就能方便地進(jìn)行測(cè)試。如果運(yùn)行于Windows平臺(tái),請(qǐng)?jiān)囈辉囄④浌镜腤eb服務(wù)器iis。
FSO 模型對(duì)象
Drive Object:驅(qū)動(dòng)器對(duì)象 供存取磁盤(pán)或者網(wǎng)絡(luò)驅(qū)動(dòng)器
FileSystemObject Object:文件系統(tǒng)對(duì)象 供存取計(jì)算機(jī)的文件系統(tǒng)
Folder Object:文件夾對(duì)象 供存取文件夾的所有屬性
TextStream Object:文本流對(duì)象 供存取文件內(nèi)容
你可以使用上面的對(duì)象做計(jì)算機(jī)上的任何事情,也包括破壞活動(dòng) ;-( 所以,請(qǐng)小心使用FSO。在web環(huán)境中,存儲(chǔ)信息是非常重要的,比如用戶信息,日志文件,等等。FSO提供了一個(gè)強(qiáng)大且簡(jiǎn)單的方法高效率地保存數(shù)據(jù)。FSO由微軟公司提供支持,對(duì)于非Windows系統(tǒng),大概不能再使用ASP。
1.文件操作,取文件大小
Function GetFileSize(FileName)'//功能:取文件大小'//形參:文件名'//返回值:成功為文件大小,失敗為-1'//Dim fIf ReportFileStatus(FileName) = 1 ThenSet f = fso.Getfile(FileName)GetFileSize = f.SizeElseGetFileSize = -1End ifEnd Function
2.使用FSO刪除指定文件
Function deleteAFile(filespec)'//功能:文件刪除'//形參:文件名'//返回值:成功為1,失敗為-1'//If ReportFileStatus(filespec) = 1 Thenfso.deleteFile(filespec)deleteAFile = 1ElsedeleteAFile = -1End ifEnd Function
3.FSO顯示指定目錄下的所有文件
Function ShowFileList(folderspec)'//功能:目錄存在時(shí)顯示此目錄下的所有文件'//形參:目錄名'//返回值:成功為文件列表,失敗為-1'//Dim f, f1, fc, sIf ReportFolderStatus(folderspec) = 1 ThenSet f = fso.GetFolder(folderspec)Set fc = f.FilesFor Each f1 in fcs = s & f1.names = s & "|"NextShowFileList = sElseShowFileList = -1End ifEnd Function
4.使用fso復(fù)制指定文件
Function CopyAFile(SourceFile,DestinationFile)'//功能:源文件存在時(shí),才能對(duì)文件進(jìn)行復(fù)制,目的文件無(wú)影響'//形參:源文件,目的文件'//返回值:成功為1,失敗為-1'//Dim MyFileIf ReportFileStatus(SourceFile) = 1 ThenSet MyFile = fso.GetFile(SourceFile)MyFile.Copy (DestinationFile)CopyAFile = 1ElseCopyAFile = -1End ifEnd Function
5.源文件存在時(shí)目的文件不存在時(shí)才能對(duì)文件進(jìn)行移動(dòng)
'Response.Write MoveAFile("f:/123/4561.exe","f:/123/4562.txt")Function MoveAFile(SourceFile,DestinationFile)'//形參:源文件,目的文件'//返回值:成功為1,失敗為-1'//If ReportFileStatus(SourceFile)=1 AndReportFileStatus(DestinationFileORPath) =-1 Thenfso.MoveFile SourceFile,DestinationFileORPathMoveAFile = 1ElseMoveAFile = -1End ifEnd Function 6.FSO判斷指定文件是否存在?
Function ReportFileStatus(FileName)'//功能:判斷文件是否存在'//形參:文件名'//返回值:成功為1,失敗為-1'//Dim msgmsg = -1If (fso.FileExists(FileName)) Thenmsg = 1Elsemsg = -1End IfReportFileStatus = msgEnd Function
7.FSO讀取文件創(chuàng)建日期
Function ShowDatecreated(filespec)'//功能:文件創(chuàng)建日期'//形參:文件名'//返回值:成功:文件創(chuàng)建日期,失敗:-1'//Dim fIf ReportFileStatus(filespec) = 1 ThenSet f = fso.GetFile(filespec)ShowDatecreated = f.DatecreatedElseShowDatecreated = -1End ifEnd Function
8.FSO顯示文件讀寫(xiě)權(quán)限屬性
Function GetAttributes(FileName)'//功能:顯示文件屬性'//形參:文件名'//返回值:成功:文件屬性,失敗:-1'//Dim f,StrIf ReportFileStatus(FileName) = 1 ThenSet f = fso.GetFile(FileName)select Case f.attributesCase 0 Str="普通文件。沒(méi)有設(shè)置任何屬性。 "Case 1 Str="只讀文件。可讀寫(xiě)。 "Case 2 Str="隱藏文件。可讀寫(xiě)。 "Case 4 Str="系統(tǒng)文件。可讀寫(xiě)。 "Case 16 Str="文件夾或目錄。只讀。 "Case 32 Str="上次備份后已更改的文件。可讀寫(xiě)。 "Case 1024 Str="鏈接或快捷方式。只讀。 "Case 2048 Str=" 壓縮文件。只讀。"End selectGetAttributes = StrElseGetAttributes = -1End ifEnd Function
9.FSO顯示指定文件最后一次訪問(wèn)/最后一次修改時(shí)間
'Response.Write ShowFileAccessInfo("文件路徑")Function ShowFileAccessInfo(FileName,InfoType)'//功能:顯示文件創(chuàng)建時(shí)信息'//形參:文件名,信息類別'// 1 -----創(chuàng)建時(shí)間'// 2 -----上次訪問(wèn)時(shí)間'// 3 -----上次修改時(shí)間'// 4 -----文件路徑'// 5 -----文件名稱'// 6 -----文件類型'// 7 -----文件大小'// 8 -----父目錄'// 9 -----根目錄'//返回值:成功為文件創(chuàng)建時(shí)信息,失敗:-1'//Dim f, sIf ReportFileStatus(FileName) = 1 thenSet f = fso.GetFile(FileName)select Case InfoTypeCase 1 s = f.Datecreated '// 1 -----創(chuàng)建時(shí)間Case 2 s = f.DateLastAccessed '// 2 -----上次訪問(wèn)時(shí)間Case 3 s = f.DateLastModified '// 3 -----上次修改時(shí)間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 selectShowFileAccessInfo = sELseShowFileAccessInfo = -1End ifEnd Function 10.FSO寫(xiě)指定內(nèi)容到文本文件
Function WriteTxtFile(FileName,TextStr,WriteORAppendType)Const ForReading = 1, ForWriting = 2 , ForAppending = 8Dim f, mselect Case WriteORAppendTypeCase 1: '文件進(jìn)行寫(xiě)操作Set f = fso.OpenTextFile(FileName, ForWriting, True)f.Write TextStrf.CloseIf ReportFileStatus(FileName) = 1 thenWriteTxtFile = 1ElseWriteTxtFile = -1End ifCase 2: '文件末尾進(jìn)行寫(xiě)操作If ReportFileStatus(FileName) = 1 thenSet f = fso.OpenTextFile(FileName, ForAppending)f.Write TextStrf.CloseWriteTxtFile = 1ElseWriteTxtFile = -1End ifEnd selectEnd Function
11.利用FSO讀取文本文件內(nèi)容
Function ReadTxtFile(FileName)Const ForReading = 1, ForWriting = 2Dim f, mIf ReportFileStatus(FileName) = 1 thenSet f = fso.OpenTextFile(FileName, ForReading)m = f.ReadLine'm = f.ReadAll'f.SkipLineReadTxtFile = mf.CloseElseReadTxtFile = -1End ifEnd Function
12.FSO返回文件夾目錄空間大小
Function GetFolderSize(FolderName)'//功能:取目錄大小'//形參:目錄名'//返回值:成功為目錄大小,失敗為-1'//Dim fIf ReportFolderStatus(FolderName) = 1 ThenSet f = fso.GetFolder(FolderName)GetFolderSize = f.SizeElseGetFolderSize = -1End ifEnd Function
13.使用FSO創(chuàng)建文件夾
Function createFolderDemo(FolderName)'//功能:創(chuàng)建的文件夾'//形參:目錄名'//返回值:成功為1,失敗為-1'//Dim fIf ReportFolderStatus(Folderspec) = 1 ThencreateFolderDemo = -1ElseSet f = fso.createFolder(FolderName)createFolderDemo = 1End ifEnd Function
14.FSO刪除指定文件夾目錄
Function deleteAFolder(Folderspec)'//功能:目錄刪除'//形參:目錄名'//返回值:成功為1,失敗為-1'//Response.write FolderspecIf ReportFolderStatus(Folderspec) = 1 Thenfso.deleteFolder (Folderspec)deleteAFolder = 1ElsedeleteAFolder = -1End ifEnd Function
15.FSO顯示指定目錄的文件夾目錄列表
Function ShowFolderList(folderspec)'//功能:目錄存在時(shí)顯示此目錄下的所有子目錄'//形參:目錄名'//返回值:成功為子目錄列表,失敗為-1'//Dim f, f1, fc, sIf ReportFolderStatus(folderspec) = 1 ThenSet f = fso.GetFolder(folderspec)Set fc = f.SubFoldersFor Each f1 in fcs = s & f1.names = s & "|"NextShowFolderList = sElseShowFolderList = -1End ifEnd Function
16.FSO復(fù)制指定文件夾目錄
Function CopyAFolder(SourceFolder,DestinationFolder)'//功能:源目錄存在時(shí),才能對(duì)目錄進(jìn)行復(fù)制,目的目錄無(wú)影響'//形參:源目錄,目的目錄'//返回值:成功為1,失敗為-1'//Dim MyFolderIf ReportFolderStatus(SourceFolder) = 1 and ReportFolderStatus(DestinationFolder) = -1 ThenSet MyFolder = fso.GetFolder(SourceFolder)fso.CopyFolder SourceFolder,DestinationFolderCopyAFolder = 1ElseCopyAFolder = -1End ifEnd Function
17.移動(dòng)指定文件夾目錄
Function MoveAFolder(SourcePath,DestinationPath)'//功能:源目錄存在時(shí)目的目錄不存在時(shí)才能對(duì)目錄進(jìn)行移動(dòng)'//形參:源目錄,目的目錄'//返回值:成功為1,失敗為-1'//If ReportFolderStatus(SourcePath)=1 And ReportFolderStatus(DestinationPath)=0 Thenfso.MoveFolder SourcePath, DestinationPathMoveAFolder = 1ElseMoveAFolder = -1End ifEnd Function
18.判斷某目錄是否存在
'Response.Write ReportFolderStatus("G:/soft/delphi/my_pro/")Function ReportFolderStatus(fldr)'//功能:判斷目錄是否存在'//形參:目錄'//返回值:成功為1,失敗為-1'//Dim msgmsg = -1If (fso.FolderExists(fldr)) Thenmsg = 1Elsemsg = -1End IfReportFolderStatus = msgEnd Function19.顯示目錄創(chuàng)建時(shí)信息
Function ShowFolderAccessInfo(FolderName,InfoType)'//功能:顯示目錄創(chuàng)建時(shí)信息'//形參:目錄名,信息類別'// 1 -----創(chuàng)建時(shí)間'// 2 -----上次訪問(wèn)時(shí)間'// 3 -----上次修改時(shí)間'// 4 -----目錄路徑'// 5 -----目錄名稱'// 6 -----目錄類型'// 7 -----目錄大小'// 8 -----父目錄'// 9 -----根目錄'//返回值:成功為目錄創(chuàng)建時(shí)信息,失敗:-1'//Dim f, sIf ReportFolderStatus(FolderName) = 1 thenSet f = fso.GetFolder(FolderName)select Case InfoTypeCase 1 s = f.Datecreated '// 1 -----創(chuàng)建時(shí)間Case 2 s = f.DateLastAccessed '// 2 -----上次訪問(wèn)時(shí)間Case 3 s = f.DateLastModified '// 3 -----上次修改時(shí)間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 selectShowFolderAccessInfo = sELseShowFolderAccessInfo = -1End ifEnd Function
20.返回文件夾嵌套數(shù)
Function DisplayLevelDepth(pathspec)Dim f, n ,PathSet f = fso.GetFolder(pathspec)If f.IsRootFolder ThenDisplayLevelDepth ="指定的文件夾是根文件夾。"&RootFolderElseDo Until f.IsRootFolderPath = Path & f.Name &""Set f = f.ParentFoldern = n + 1LoopDisplayLevelDepth ="指定的文件夾是嵌套級(jí)為 " & n & "的文件夾。"&PathEnd IfEnd Function
21.判斷指定磁盤(pán)驅(qū)動(dòng)器是否存在?
'Response.Write ReportDriveStatus("C:/")Function ReportDriveStatus(drv)'//功能:判斷磁盤(pán)是否存在'//形參:磁盤(pán)'//返回值:成功為1,失敗為-1'//Dim msgmsg = -1If fso.DriveExists(drv) Thenmsg = 1Elsemsg = -1End IfReportDriveStatus = msgEnd Function22.FSO返回指定磁盤(pán)可用的類型包括 FAT、NTFS 和 CDFS。
'Response.Write ShowFileSystemType("C:/")Function ShowFileSystemType(drvspec)'//功能:磁盤(pán)類型'//形參:磁盤(pán)名'//返回值:成功為類型:FAT、NTFS 和 CDFS,失敗:-1'//Dim dIf ReportDriveStatus(drvspec) = 1 ThenSet d = fso. GetDrive(drvspec)ShowFileSystemType = d.FileSystemELseShowFileSystemType = -1End ifEnd Function新聞熱點(diǎn)
疑難解答
圖片精選