创建文本文件
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