vbs创建日志文件并追加内容

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

'创建文件

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = objFSO.CreateTextFile("C:\FSO\FSO.txt")

'往文件里写内容

LogFile = "C:\FSO\FSO.txt"
Const ForWriting = 2 '2为重写文件,8为追加
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(LogFile,ForWriting)
objFile.WriteLine "beginning process  " &Now
objFile.WriteLine "working on process  "&Now
objFile.WriteLine "Process completed at  "&Now

objFile.Close
MsgBox "写入完成"

'验证文件是否存在

LogFile = "C:\FSO\FSO.txt"
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
 If objFSO.FileExists(LogFile) Then
    MsgBox LogFile&"  存在,执行写操作步骤"
    Set objFile = objFSO.OpenTextFile(LogFile,ForAppending)
        objFile.Write "appending  "&Now
    MsgBox "成功写入"
 Else
    MsgBox LogFile&" 不存在,执行先创建此文件,然后写操作"
    Set objFile = objFSO.CreateTextFile(LogFile)
        objFile.Write "writing to new file  "&Now
    MsgBox "创建并写入成功"
 End If

 

转载于:https://my.oschina.net/u/2391658/blog/709284

你可能感兴趣的:(vbs创建日志文件并追加内容)