FSO - FileSystemObject 或 Scripting.FileSystemObject 的縮寫,為 IIS 內置組件,用于操作磁盤、文件夾或文本文件。FSO 的對象、方法和屬性非常的多,這里用示例的方式列出常用的,如果您要查看更詳盡的信息,請點擊這里下載 FileSystemObject 參考,注意:《VBScript 語言參考》或《JScript 語言參考》中的:《FileSystemObject 用戶指南》和《Scripting 運行時庫參考》便是微軟給出的 FileSystemObject 完整參考。

          FSO 不能操作二進制文件,要操作二進制文件,請使用:ADODB.Stream。

          創(chuàng)建文件
          dim fso, f
          set fso = server.CreateObject("Scripting.FileSystemObject")
          set f = fso.CreateTextFile("C:\test.txt", true) '第二個參數(shù)表示目標文件存在時是否覆蓋
          f.Write("寫入內容")
          f.WriteLine("寫入內容并換行")
          f.WriteBlankLines(3) '寫入三個空白行(相當于在文本編輯器中按三次回車)
          f.Close()
          set f = nothing
          set fso = nothing

          打開并讀文件
          dim fso, f
          set fso = server.CreateObject("Scripting.FileSystemObject")
          set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二個參數(shù) 1 表示只讀打開,第三個參數(shù)表示目標文件不存在時是否創(chuàng)建
          f.Skip(3) '將當前位置向后移三個字符
          f.SkipLine() '將當前位置移動到下一行的第一個字符,注意:無參數(shù)
          response.Write f.Read(3) '從當前位置向后讀取三個字符,并將當前位置向后移三個字符
          response.Write f.ReadLine() '從當前位置向后讀取直到遇到換行符(不讀取換行符),并將當前位置移動到下一行的第一個字符,注意:無參數(shù)
          response.Write f.ReadAll() '從當前位置向后讀取,直到文件結束,并將當前位置移動到文件的最后
          if f.atEndOfLine then
              response.Write("一行的結尾!")
          end if
          if f.atEndOfStream then
              response.Write("文件的結尾!")
          end if
          f.Close()
          set f = nothing
          set fso = nothing

          打開并寫文件
          dim fso, f
          set fso = server.CreateObject("Scripting.FileSystemObject")
          set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二個參數(shù) 2 表示重寫,如果是 8 表示追加
          f.Write("寫入內容")
          f.WriteLine("寫入內容并換行")
          f.WriteBlankLines(3) '寫入三個空白行(相當于在文本編輯器中按三次回車)
          f.Close()
          set f = nothing
          set fso = nothing

          判斷文件是否存在
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          if fso.FileExists("C:\test.txt") then
              response.Write("目標文件存在")
          else
              response.Write("目標文件不存在")
          end if
          set fso = nothing

          移動文件
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          call fso.MoveFile("C:\test.txt", "D:\test111.txt") '兩個參數(shù)的文件名部分可以不同
          set fso = nothing

          復制文件
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          call fso.CopyFile("C:\test.txt", "D:\test111.txt") '兩個參數(shù)的文件名部分可以不同
          set fso = nothing

          刪除文件
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          fso.DeleteFile("C:\test.txt")
          set fso = nothing

          創(chuàng)建文件夾
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          fso.CreateFolder("C:\test") '目標文件夾的父文件夾必須存在
          set fso = nothing

          判斷文件夾是否存在
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          if fso.FolderExists("C:\Windows") then
              response.Write("目標文件夾存在")
          else
              response.Write("目標文件夾不存在")
          end if
          set fso = nothing

          刪除文件夾
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          fso.DeleteFolder("C:\test") '文件夾不必為空
          set fso = nothing
          CreateTextFile
          (filename,overwrite,unicode) 
          用指定的文件名在文件夾內創(chuàng)建一個新的文本文件,并且返回一個相應的TextStream對象。如果可選的overwrite參數(shù)設置為True,將覆蓋任何已有的同名文件。缺省的overwrite參數(shù)是False。如果可選的unicode參數(shù)設置為True,文件的內容將存儲為unicode文本。缺省的unicode是False 

                 在文件夾之間可以使用當前文件夾的ParentFolder屬性,返回到父目錄。當?shù)竭_一個文件夾時,如果IsRootFolder屬性是True,就停下來。離開驅動器的根目錄,沿目錄樹向下,可遍歷或訪問在Folders集合(由當前文件夾的SubFolders屬性返回)內的指定文件夾。
                 下列程序遍歷了驅動器C根目錄內的所有文件夾,并顯示各個文件夾的有關信息。
                 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>');
          }

          posted on 2009-01-07 15:30 sanmao 閱讀(118) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 白银市| 兰西县| 电白县| 宁蒗| 宁城县| 桦甸市| 视频| 平果县| 开鲁县| 上蔡县| 凤翔县| 华池县| 利川市| 枣强县| 旌德县| 阳东县| 北流市| 龙州县| 壤塘县| 昭平县| 莱西市| 敖汉旗| 大田县| 丰县| 巴林右旗| 莆田市| 江津市| 咸丰县| 徐水县| 华蓥市| 浦东新区| 樟树市| 高陵县| 金山区| 渭源县| 武宁县| 新晃| 江永县| 晴隆县| 庄河市| 梅河口市|