有兩種主要的檔案處理類型:
建立、添加或刪除資料,以及讀取檔案
移動、複製和刪除檔案
建立檔案
建立空文字檔(有時被叫做“文字資料流”)有三種方法。
第一種方法是用 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!");
}