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

          FSO 不能操作二進(jìn)制文件,要操作二進(jìn)制文件,請(qǐng)使用:ADODB.Stream。

          創(chuàng)建文件
          dim fso, f
          set fso = server.CreateObject("Scripting.FileSystemObject")
          set f = fso.CreateTextFile("C:\test.txt", true) '第二個(gè)參數(shù)表示目標(biāo)文件存在時(shí)是否覆蓋
          f.Write("寫入內(nèi)容")
          f.WriteLine("寫入內(nèi)容并換行")
          f.WriteBlankLines(3) '寫入三個(gè)空白行(相當(dāng)于在文本編輯器中按三次回車)
          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) '第二個(gè)參數(shù) 1 表示只讀打開,第三個(gè)參數(shù)表示目標(biāo)文件不存在時(shí)是否創(chuàng)建
          f.Skip(3) '將當(dāng)前位置向后移三個(gè)字符
          f.SkipLine() '將當(dāng)前位置移動(dòng)到下一行的第一個(gè)字符,注意:無參數(shù)
          response.Write f.Read(3) '從當(dāng)前位置向后讀取三個(gè)字符,并將當(dāng)前位置向后移三個(gè)字符
          response.Write f.ReadLine() '從當(dāng)前位置向后讀取直到遇到換行符(不讀取換行符),并將當(dāng)前位置移動(dòng)到下一行的第一個(gè)字符,注意:無參數(shù)
          response.Write f.ReadAll() '從當(dāng)前位置向后讀取,直到文件結(jié)束,并將當(dāng)前位置移動(dòng)到文件的最后
          if f.atEndOfLine then
              response.Write("一行的結(jié)尾!")
          end if
          if f.atEndOfStream then
              response.Write("文件的結(jié)尾!")
          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) '第二個(gè)參數(shù) 2 表示重寫,如果是 8 表示追加
          f.Write("寫入內(nèi)容")
          f.WriteLine("寫入內(nèi)容并換行")
          f.WriteBlankLines(3) '寫入三個(gè)空白行(相當(dāng)于在文本編輯器中按三次回車)
          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("目標(biāo)文件存在")
          else
              response.Write("目標(biāo)文件不存在")
          end if
          set fso = nothing

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

          復(fù)制文件
          dim fso
          set fso = server.CreateObject("Scripting.FileSystemObject")
          call fso.CopyFile("C:\test.txt", "D:\test111.txt") '兩個(gè)參數(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") '目標(biāo)文件夾的父文件夾必須存在
          set fso = nothing

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

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

                 在文件夾之間可以使用當(dāng)前文件夾的ParentFolder屬性,返回到父目錄。當(dāng)?shù)竭_(dá)一個(gè)文件夾時(shí),如果IsRootFolder屬性是True,就停下來。離開驅(qū)動(dòng)器的根目錄,沿目錄樹向下,可遍歷或訪問在Folders集合(由當(dāng)前文件夾的SubFolders屬性返回)內(nèi)的指定文件夾。
                 下列程序遍歷了驅(qū)動(dòng)器C根目錄內(nèi)的所有文件夾,并顯示各個(gè)文件夾的有關(guān)信息。
                 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) 評(píng)論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 周口市| 班玛县| 凤阳县| 天祝| 和田市| 东乡族自治县| 苗栗市| 酉阳| 沙湾县| 乳山市| 河东区| 宁南县| 开化县| 孟连| 淮南市| 英德市| 阿克陶县| 庄浪县| 昔阳县| 乾安县| 拜泉县| 双鸭山市| 云霄县| 新乡市| 禹州市| 如皋市| 屯留县| 平遥县| 永川市| 梁平县| 溧水县| 互助| 长垣县| 三江| 宜兴市| 桃江县| 湖州市| 平阴县| 遂平县| 云南省| 和静县|