' ===================================================
' 以下为文件操作函数
' ===================================================
Sub InitNotepad(MyfileName)
'
'初始化文件,判断文件MyfileName存在不,不存在则创建,存在则清空;
'
Dim fso, fil
'创建fso对象
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.fileexists(MyfileName)) Then
'打开文件,参数1为forreading,2为forwriting,8为appending
'如果存在文件则删除该文件
fso.DeleteFile (MyfileName)
End If
Set fil = fso.CreateTextFile(MyfileName, True)
'释放文件
Set fso = Nothing
End Sub
Sub AppendNotepad(MyfileName, MyStr)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, fil
'创建fso对象
Set fso = CreateObject("Scripting.FileSystemObject")
'TristateUseDefault –2 使用系统缺省打开文件。
'TristateTrue –1 以 Unicode 格式打开文件。
'TristateFalse 0 以 ASCII 格式打开文件。
'注意这里要与创建文件时的编码一致
Set fil = fso.OpenTextFile(MyfileName, 8, True, 0)
fil.writeline (MyStr)
'关闭文件
fil.Close
'释放文件
Set fil = Nothing
Set fso = Nothing
End Sub
Sub WriteNotepad(MyfileName, MyStr)
Dim fso, fil
'创建fso对象
Set fso = CreateObject("Scripting.FileSystemObject")
'初始化并写文件
Call InitNotepad(MyfileName)
Set fil = fso.OpenTextFile(MyfileName, 2)
fil.Write (MyStr)
'关闭文件
fil.Close
'释放文件
Set fil = Nothing
Set fso = Nothing
End Sub
'从宽字节到多字节
Public Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Public Const CP_UTF8 = 65001
Sub WriteOut(strPath, str)
Dim lBufSize As Long
Dim lRest As Long
Dim bUTF8() As Byte
Dim TLen As Long
TLen = Len(str)
lBufSize = TLen * 3 + 1
ReDim bUTF8(lBufSize - 1)
lRest = WideCharToMultiByte(CP_UTF8, 0, StrPtr(str), TLen, bUTF8(0), lBufSize, vbNullString, 0)
If lRest Then
lRest = lRest - 1
ReDim Preserve bUTF8(lRest)
Open strPath For Binary As #1
Put #1, , bUTF8
Close #1
End If
End Sub
'写的格式为UTF8
Sub createUFT8(strPath, str)
'换行建议使用vbcrlf,否则robotFramwork无法解析
Dim objStream As Object
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = 2 'adTypeText
.Charset = "UTF-8"
.Open
.WriteText str
.SaveToFile strPath, 2 'adSaveCreateOverWrite
End With
Set objStream = Nothing
End Sub
' ===================================================
' 以上为文件操作函数
' ===================================================