婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > FileSystemObject處理文件

FileSystemObject處理文件

熱門標簽:怎么在地圖標注自己 外呼系統API接口 萊西電子地圖標注 武夷山旅游地圖標注 個人可以辦理400電話么 縣域地圖標注打印店 金昌電話機器人價格 鳳臺百度地圖標注店 修改地圖標注
有兩種主要的文件處理類型:

創建、添加或刪除數據,以及讀取文件
移動、復制和刪除文件
創建文件
創建空文本文件(有時被叫做“文本流”)有三種方法。
第一種方法是用 CreateTextFile 方法。 下面的示例示范了在 VBScript 中如何用這種方法來創建文本文件:


Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)

要在 JScript 中用這種方法,則使用下面的代碼:

var fso, f1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);

請考察示例代碼,來領會如何在 FileSystemObject 中使用 CreateTextFile 方法。
創建文本文件的第二種方法是,使用 FileSystemObject 對象的 OpenTextFile 方法,并設置 ForWriting 標志。在 VBScript 中,代碼就像下面的示例一樣:

Dim fso, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting. FileSystemObject")
Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True)

要在 JScript 中使用這種方法來創建文本文件,則使用下面的代碼:

var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("c:\\test.txt", ForWriting, true);

創建文本文件的第三種方法是,使用 OpenAsTextStream 方法,并設置 ForWriting 標志。要使用這種方法,在 VBScript 中使用下面的代碼:

Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\test1.txt")
Set f1 = fso.GetFile("c:\test1.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)

在 JScript 中,則使用下面示例中的代碼:

var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateTextFile ("c:\\test1.txt");
f1 = fso.GetFile("c:\\test1.txt");
ts = f1.OpenAsTextStream(ForWriting, true);

添加數據到文件中
一旦創建了文本文件,使用下面的三個步驟向文件添加數據:

打開文本文件。
寫入數據。
關閉文件。
要打開現有的文件,則使用 FileSystemObject 對象的 OpenTextFile 方法或 File 對象的 OpenAsTextStream 方法。
要寫數據到打開的文本文件,則根據下表所述任務使用 TextStream 對象的 Write、WriteLine 或 WriteBlankLines 方法。

任務 方法
向打開的文本文件寫數據,不用后續一個新行字符。 Write
向打開的文本文件寫數據,后續一個新行字符。 WriteLine
向打開的文本文件寫一個或多個空白行。 WriteBlankLines


請考察示例代碼,來領會如何在 FileSystemObject 對象中使用 Write、WriteLine 和 WriteBlankLines 方法。

要關閉一個打開的文件,則使用 TextStream 對象的 Close 方法。

請考察示例代碼,來領會如何在 FileSystemObject 中使用 Close 方法。


--------------------------------------------------------------------------------

注意 新行字符包含一個或幾個字符(取決于操作系統),以把光標移動到下一行的開始位置(回車/換行)。注意某些字符串末尾可能已經有這個非打印字符了。

--------------------------------------------------------------------------------


下面的 VBScript 例子示范了如何打開文件,和同時使用三種寫方法來向文件添加數據,然后關閉文件:


Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\testfile.txt", True)
' 寫一行,并且帶有新行字符。
tf.WriteLine("Testing 1, 2, 3.")
' 向文件寫三個新行字符。
tf.WriteBlankLines(3)
' 寫一行。
tf.Write ("This is a test.")
tf.Close
End Sub
這個示例示范了在 JScript 中如何使用這三個方法:

function CreateFile()
{
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("c:\\testfile.txt", true);
// 寫一行,并且帶有新行字符。
tf.WriteLine("Testing 1, 2, 3.") ;
// 向文件寫三個新行字符。
tf.WriteBlankLines(3) ;
// 寫一行。
tf.Write ("This is a test.");
tf.Close();
}
讀取文件
要從文本文件讀取數據,則使用 TextStream 對象的 Read、ReadLine 或 ReadAll 方法。下表描述了不同的任務應使用哪種方法。
任務 方法
從文件讀取指定數量的字符。 Read
讀取一整行(一直到但不包括新行字符)。 ReadLine
讀取文本文件的整個內容。 ReadAll


請考察示例代碼,來領會如何在 FileSystemObject 中使用 ReadAll 和 ReadLine 方法。

如果使用 Read 或 ReadLine 方法,并且想跳過數據的特殊部分,則使用 Skip 或 SkipLine 方法。read 方法的結果文本存在一個字符串中,該字符串可以顯示在一個控件中,也可以用字符串函數(如 Left、Right 和 Mid)來分析,連接等等。

下面的 VBScript 示例示范了如何打開文件,和如何寫數據到文件中并從文件讀取數據:


Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
' 寫一行。
Response.Write "Writing file br>"
f1.WriteLine "Hello World"
f1.WriteBlankLines(1)
f1.Close
' 讀取文件的內容。
Response.Write "Reading file br>"
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
s = ts.ReadLine
Response.Write "File contents = '" s "'"
ts.Close
End Sub

下面的代碼示范了在 JScript 中做同樣的事:

function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
// 寫一行。
Response.Write("Writing file br>");
f1.WriteLine("Hello World");
f1.WriteBlankLines(1);
f1.Close();
// 讀取文件的內容。
Response.Write("Reading file br>");
ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();
}

移動、復制和刪除文件
FSO 對象模式各有兩種方法移動、復制和刪除文件,如下表所述。
任務 方法
移動文件 File.Move 或 FileSystemObject.MoveFile
復制文件 File.Copy 或 FileSystemObject.CopyFile
刪除文件 File.Delete 或 FileSystemObject.DeleteFile


請考察示例代碼,來領會在 FileSystemObject 中刪除文件的兩種方法。

下面的 VBScript 示例,在驅動器 C 的根目錄中創建一個文本文件,向其中寫一些信息,然后把它移動到 \tmp 目錄中,并在 \temp 中做一個備份,最后把它們從兩個目錄中刪掉。

要運行下面的示例,需要先在驅動器 C 的根目錄中創建 \tmp 和 \temp 目錄:


Sub ManipFiles
Dim fso, f1, f2, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
Response.Write "Writing file br>"
' 寫一行。
f1.Write ("This is a test.")
' 關閉文件。
f1.Close
Response.Write "Moving file to c:\tmp br>"
' 獲取 C 的根目錄(C:\)中的文件的句柄。
Set f2 = fso.GetFile("c:\testfile.txt")
' 把文件移動到 \tmp 目錄。
f2.Move ("c:\tmp\testfile.txt")
Response.Write "Copying file to c:\temp br>"
' 把文件復制到 \temp 目錄。
f2.Copy ("c:\temp\testfile.txt")
Response.Write "Deleting files br>"
' 獲得文件當前位置的句柄。
Set f2 = fso.GetFile("c:\tmp\testfile.txt")
Set f3 = fso.GetFile("c:\temp\testfile.txt")
' 刪除文件。
f2.Delete
f3.Delete
Response.Write "All done!"
End Sub

下面的代碼示范了在 JScript 中做同樣的事:

function ManipFiles()
{
var fso, f1, f2, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
Response.Write("Writing file br>");
// 寫一行。
f1.Write("This is a test.");
// 關閉文件。
f1.Close();
Response.Write("Moving file to c:\\tmp br>");
// 獲取 C 的根目錄(C:\)中的文件的句柄。
f2 = fso.GetFile("c:\\testfile.txt");
// 把文件移動到 \tmp 目錄。
f2.Move ("c:\\tmp\\testfile.txt");
Response.Write("Copying file to c:\\temp br>");
// 把文件復制到 \temp 目錄。
f2.Copy ("c:\\temp\\testfile.txt");
Response.Write("Deleting files br>");
// 獲得文件當前位置的句柄。
f2 = fso.GetFile("c:\\tmp\\testfile.txt");
f3 = fso.GetFile("c:\\temp\\testfile.txt");
// 刪除文件。
f2.Delete();
f3.Delete();
Response.Write("All done!");
}

標簽:上海 邢臺 南京 楚雄 赤峰 清遠 通遼 涼山

巨人網絡通訊聲明:本文標題《FileSystemObject處理文件》,本文關鍵詞  FileSystemObject,處理,文件,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《FileSystemObject處理文件》相關的同類信息!
  • 本頁收集關于FileSystemObject處理文件的相關信息資訊供網民參考!
  • 推薦文章

    上一篇:文本搜索

    下一篇:設計 FileSystemObject

    主站蜘蛛池模板: 清新县| 奉新县| 彩票| 玉环县| 邯郸市| 宜都市| 宁都县| 临高县| 武冈市| 庆城县| 安化县| 正宁县| 陆丰市| 禹州市| 宁国市| 关岭| 淄博市| 通渭县| 文化| 崇礼县| 揭东县| 洪湖市| 泰和县| 梁河县| 安远县| 枣强县| 辽宁省| 红河县| 连山| 安图县| 铜川市| 苍溪县| 正镶白旗| 河曲县| 五峰| 乾安县| 汉川市| 宾川县| 忻城县| 金秀| 朝阳市|