Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile("C:/FSO/ScriptLog.txt")
檢察文件是否存在
復制代碼 代碼如下:
Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists("C:/FSO/ScriptLog.txt") Then Set objFolder = objFSO.GetFile("C:/FSO/ScriptLog.txt") Else Wscript.Echo "File does not exist." End If
刪除文本文件
復制代碼 代碼如下:
Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.DeleteFile("C:/FSO/ScriptLog.txt")
重命名文件
復制代碼 代碼如下:
Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.MoveFile "C:/FSO/ScriptLog.txt" , "C:/FSO/BackupLog.txt"
文本操作 讀取全部內容
復制代碼 代碼如下:
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:/Scripts/Test.txt", ForReading) strContents = objFile.ReadAll objFile.Close
一行行的讀取文本文件內容
復制代碼 代碼如下:
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile ("c:/scripts/servers.txt", ForReading) Do Until objTextFile.AtEndOfStream strComputer = objTextFile.ReadLine Wscript.Echo strComputer Loop objTextFile.Close
追加文本文件一行內容
復制代碼 代碼如下:
Const ForAppending = 8 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile ("C:/Scripts/Service_Status.txt", ForAppending, True) objTextFile.WriteLine("追加的內容") objTextFile.Close
有用的幾個函數: 替換:將Jim替換成James。
復制代碼 代碼如下:
strNewText = Replace(strText, "Jim ", "James ")
用逗號分隔字符串:
復制代碼 代碼如下:
arrpath=split(strDN,",") wscript.echo arrpath(0)
幾個實例: 讀取文本文件指定的行內容(讀第四行內容存到strLine變量中)
復制代碼 代碼如下:
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("mylogfile.log", ForReading) For i = 1 to 3 objTextFile.ReadLine Next strLine = objTextFile.ReadLine Wscript.Echo strLine objTextFile.Close