标题: [技术讨论] vbs如何创建一个utf-8无签名格式文本?(已解决) [打印本页]
作者: 窄口牛 时间: 2023-1-9 07:07 标题: vbs如何创建一个utf-8无签名格式文本?(已解决)
本帖最后由 窄口牛 于 2023-2-20 07:58 编辑
搜到的例子是utf-8有签名格式的。
作者: Batcher 时间: 2023-1-9 09:15
回复 1# 窄口牛
能否提供一个你搜到的例子参考一下?
作者: czjt1234 时间: 2023-1-9 17:06
有签名格式,是不是指BOM ?
如果是要无BOM的,那就随便建一个0字节的空文件就行了
关键在于软件以哪种方式打开,ANSI还是UTF-8
作者: czjt1234 时间: 2023-1-9 22:05
- Function remove_utf8_BOM(ByVal file)
- Dim oStream, s, i
- Set oStream = CreateObject("ADODB.Stream")
- oStream.Type = 1 'adTypeBinary
- oStream.Mode = 3 'adModeReadWrite
- oStream.Open()
- With CreateObject("ADODB.Stream")
- .Type = 1 'adTypeBinary
- .Mode = 3 'adModeReadWrite
- .Open()
- On Error Resume Next
- Err.Clear()
- .LoadFromFile file
- If Err.Number <> 0 Then remove_utf8_BOM = Err.Description : Exit Function
- If .Size < 3 Then remove_utf8_BOM = "False" : Exit Function 'no utf-8 BOM
- s = ""
- For i = 0 To 2
- s = s & Hex(AscB(.Read(1)))
- Next
- If s <> "EFBBBF" Then remove_utf8_BOM = "False" : Exit Function 'no utf-8 BOM
- .Position = 3
- .CopyTo oStream
- .Close()
- End With
- Err.Clear()
- oStream.SaveToFile file, 2 'adSaveCreateOverWrite
- If Err.Number <> 0 Then remove_utf8_BOM = Err.Description : Exit Function
- oStream.Close()
- remove_utf8_BOM = "True" 'write file complete
- End Function
复制代码
这个用于去除utf-8文件的BOM
作者: 窄口牛 时间: 2023-1-10 08:54
本帖最后由 窄口牛 于 2023-1-10 09:51 编辑
打开方式左右不了哇,Linux下建的sh,肯定是utf8哇,空的确实是会没有区别?
事实证明空的缺数哟有问题
新建必须有内容才能确定。
作者: 窄口牛 时间: 2023-1-10 09:25
- gangzi="1.txt"
- gangzi2="www.gangzi.org"
- Set Stm1 = CreateObject("ADODB.Stream")
- Stm1.Type = 2
- Stm1.Open
- Stm1.Charset = "UTF-8"
- Stm1.Position = Stm1.Size
- Stm1.WriteText gangzi2
- Stm1.SaveToFile gangzi,2
- Stm1.Close
- set Stm1 = nothing
复制代码
作者: terse 时间: 2023-1-10 13:13
先新建 再删除BOM- var str = '批处理之家-http://bbs.bathome.net',
- path = 'a.txt',
- stream, newado;
- stream = WSH.CreateObject("ADODB.Stream");
- stream.type = 2;
- stream.charset = 'utf-8';
- stream.open();
- stream.writetext(str);
- stream.Position = 3;
- newado = WSH.CreateObject("ADODB.Stream");
- newado.Type = 1;
- newado.open();
- stream.CopyTo(newado);
- newado.SaveToFile(path, 2);
- stream.Close;
- newado.Close;
复制代码
作者: 窄口牛 时间: 2023-2-20 07:57
- Option Explicit
-
- Dim sTEXT,sFILE
-
- sFILE = "TEST_UTF8_NOBOM.TXT"
- sTEXT = "Сампле тест"
-
- Call WriteFileUTF8_NOBOM(sFILE,sTEXT)
-
- WScript.Echo(sTEXT)
-
- Private Sub WriteFileUTF8_NOBOM(sFILE,sTEXT)
- Const adSaveCreateNotExist = 1
- Const adSaveCreateOverWrite = 2
- Const adTypeBinary = 1
- Const adTypeText = 2
- Const adModeReadWrite = 3
-
- Dim oUTF8Stream,oBinaryStream
- Set oUTF8Stream = CreateObject("ADODB.Stream")
- Set oBinaryStream = CreateObject("ADODB.Stream")
-
- oUTF8Stream.Type = adTypeText
- oUTF8Stream.Charset = "utf-8"
- oUTF8Stream.Open
- oUTF8Stream.WriteText sTEXT
- oUTF8Stream.Position = 3 ' Skip BOM
-
- oBinaryStream.Type = adTypeBinary
- oBinaryStream.Mode = adModeReadWrite
- oBinaryStream.Open
-
- oUTF8Stream.CopyTo oBinaryStream
- oUTF8Stream.Flush
- oUTF8Stream.Close
-
- oBinaryStream.SaveToFile sFILE, adSaveCreateOverWrite
- oBinaryStream.Flush
- oBinaryStream.Close
-
- Set oUTF8Stream = Nothing
- Set oBinaryStream = Nothing
- End Sub
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |