创建文本文件
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.CreateTextFile("C:\FSOScriptLog.txt")
检察文件是否存在
Set bjFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\FSOScriptLog.txt") Then
Set bjFolder = objFSO.GetFile("C:\FSOScriptLog.txt")
Else
MsgBox "File does not exist."
End If
|
删除文本文件
Set bjFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\FSOScriptLog.txt")
移动文件
Set bjFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSOScriptLog.txt" , "D:\"
复制文件
Set bjFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSOScriptLog.txt" , "D:\"
重命名文件
Set bjFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.txt" , "C:\FSO\BackupLog.txt"
读取全部内容
Const ForReading = 1
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.OpenTextFile("C:ScriptsTest.txt", ForReading)
strContents = objFile.ReadAll
Wscript.Echo strContents
objFile.Close
|
一行行的读取文本文件内容
Const ForReading = 1
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile ("C: \ScriptsTest.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.ReadLine
Wscript.Echo strComputer
Loop
objTextFile.Close
|
追加文本文件一行内容
Const ForAppending = 8
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile ("C:\ScriptsTest.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 bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile("C:\ScriptsTest.txt ", ForReading)
For i = 1 to 3
objTextFile.ReadLine
Next
strLine = objTextFile.Readline
MsgBox strLine
objTextFile.Close
|
查看文件属性
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.GetFile("c:\ScriptsTest.txt")
msgbox "Date created: " & objFile.DateCreated
msgbox "Date last accessed: " & objFile.DateLastAccessed
msgbox "Date last modified: " & objFile.DateLastModified
msgbox "Drive: " & objFile.Drive
msgbox "Name: " & objFile.Name
msgbox "Parent folder: " & objFile.ParentFolder
msgbox "Path: " & objFile.Path
msgbox "Short name: " & objFile.ShortName
msgbox "Short path: " & objFile.ShortPath
msgbox "Size: " & objFile.Size
msgbox "Type: " & objFile.Type
|
修改文件属性
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.GetFile("C:\ScriptsTest.txt")
If objFile.Attributes AND 1 Then
objFile.Attributes = objFile.Attributes XOR 1
End If
|
写入文本文件
Const ForWriting=2
Set bj=createobject("scripting.filesystemobject")
Set bjfile=obj.opentextfile("C:\ScriptsTest.txt", ForWriting)
objfile.write("This is line 1.")
objfile.writeline("This is line2.")
objfile.close
|
最新内容请见作者的GitHub页:http://qaseven.github.io/