2019-05-13
0
废话也不多说了,直接上代码。
读取文件
stm.charset = "UTF-8"读取文件的编码,这里是UTF-8,如GB2312,当然也是可以读取其他编码格式的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Function ReadFile(FileUrl) Dim str Set stm = CreateObject("ADODB.stream") stm.Type = 2 stm.mode = 3 stm.charset = "UTF-8" stm.Open stm.loadfromfile FileUrl str = stm.readtext stm.Close Set stm = Nothing ReadFile = str End Function
Dim str str = ReadFile("01.txt") Msgbox str
|
创建、保存文件
这里同上,也可以储存为其他的编码,已经存在的文件会被替换掉,没有的文件则创建新文件。
相对路劲和绝对路劲都可以,没有创建的文件夹也会创建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| Set fso = WScript.CreateObject("Scripting.Filesystemobject") Sub CreateFile(FileUrl,Msg) Set stm = CreateObject("ADODB.Stream") stm.Type = 2 stm.Open stm.Charset = "UTF-8" stm.Position = stm.Size stm.WriteText Msg Dim FolderArray,Folder FileUrl = Replace(FileUrl,"/","\") FolderArray = Split(FileUrl, "\") If UBound(FolderArray) <> 0 Then For i = 0 To UBound(FolderArray)-1 Folder = Folder & FolderArray(i) If fso.folderExists(Folder) = flase Then fso.createfolder (Folder) End If Folder = Folder & "\" Next End If stm.SaveToFile FileUrl, 2 stm.Close set stm = nothing End Sub
call CreateFile("01.txt","文本信息") call CreateFile("D:\txt\02.txt","文本信息")
|