Board logo

标题: [技术讨论] 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

  1. Function remove_utf8_BOM(ByVal file)
  2.     Dim oStream, s, i
  3.     Set oStream = CreateObject("ADODB.Stream")
  4.     oStream.Type = 1    'adTypeBinary
  5.     oStream.Mode = 3    'adModeReadWrite
  6.     oStream.Open()
  7.     With CreateObject("ADODB.Stream")
  8.         .Type = 1    'adTypeBinary
  9.         .Mode = 3    'adModeReadWrite
  10.         .Open()
  11.         On Error Resume Next
  12.         Err.Clear()
  13.         .LoadFromFile file
  14.         If Err.Number <> 0 Then remove_utf8_BOM = Err.Description : Exit Function
  15.         If .Size < 3 Then remove_utf8_BOM = "False" : Exit Function  'no utf-8 BOM
  16.         s = ""
  17.         For i = 0 To 2
  18.             s = s & Hex(AscB(.Read(1)))
  19.         Next
  20.         If s <> "EFBBBF" Then remove_utf8_BOM = "False" : Exit Function  'no utf-8 BOM
  21.         .Position = 3
  22.         .CopyTo oStream
  23.         .Close()
  24.     End With
  25.     Err.Clear()
  26.     oStream.SaveToFile file, 2    'adSaveCreateOverWrite
  27.     If Err.Number <> 0 Then remove_utf8_BOM = Err.Description : Exit Function
  28.     oStream.Close()
  29.     remove_utf8_BOM = "True"    'write file complete
  30. End Function
复制代码
这个用于去除utf-8文件的BOM
作者: 窄口牛    时间: 2023-1-10 08:54

本帖最后由 窄口牛 于 2023-1-10 09:51 编辑

打开方式左右不了哇,Linux下建的sh,肯定是utf8哇,空的确实是会没有区别?
事实证明空的缺数哟有问题
新建必须有内容才能确定。
作者: 窄口牛    时间: 2023-1-10 09:25

  1. gangzi="1.txt"
  2. gangzi2="www.gangzi.org"
  3. Set Stm1 = CreateObject("ADODB.Stream")
  4. Stm1.Type = 2
  5. Stm1.Open
  6. Stm1.Charset = "UTF-8"
  7. Stm1.Position = Stm1.Size
  8. Stm1.WriteText gangzi2
  9. Stm1.SaveToFile gangzi,2
  10. Stm1.Close
  11. set Stm1 = nothing
复制代码

作者: terse    时间: 2023-1-10 13:13

先新建 再删除BOM
  1. var str = '批处理之家-http://bbs.bathome.net',
  2. path = 'a.txt',
  3. stream, newado;
  4. stream = WSH.CreateObject("ADODB.Stream");
  5. stream.type = 2;
  6. stream.charset = 'utf-8';
  7. stream.open();
  8. stream.writetext(str);
  9. stream.Position = 3;
  10. newado = WSH.CreateObject("ADODB.Stream");
  11. newado.Type = 1;
  12. newado.open();
  13. stream.CopyTo(newado);
  14. newado.SaveToFile(path, 2);
  15. stream.Close;
  16. newado.Close;
复制代码

作者: 窄口牛    时间: 2023-2-20 07:57

  1. Option Explicit
  2. Dim sTEXT,sFILE
  3. sFILE = "TEST_UTF8_NOBOM.TXT"
  4. sTEXT = "Сампле тест"
  5. Call WriteFileUTF8_NOBOM(sFILE,sTEXT)
  6. WScript.Echo(sTEXT)
  7. Private Sub WriteFileUTF8_NOBOM(sFILE,sTEXT)
  8.     Const adSaveCreateNotExist = 1
  9.     Const adSaveCreateOverWrite = 2
  10.     Const adTypeBinary = 1
  11.     Const adTypeText = 2
  12.     Const adModeReadWrite = 3
  13.     Dim oUTF8Stream,oBinaryStream
  14.     Set oUTF8Stream = CreateObject("ADODB.Stream")
  15.     Set oBinaryStream = CreateObject("ADODB.Stream")
  16.     oUTF8Stream.Type = adTypeText
  17.     oUTF8Stream.Charset = "utf-8"
  18.     oUTF8Stream.Open
  19.     oUTF8Stream.WriteText sTEXT
  20.     oUTF8Stream.Position = 3 ' Skip BOM
  21.     oBinaryStream.Type = adTypeBinary
  22.     oBinaryStream.Mode = adModeReadWrite
  23.     oBinaryStream.Open
  24.     oUTF8Stream.CopyTo oBinaryStream
  25.     oUTF8Stream.Flush
  26.     oUTF8Stream.Close
  27.     oBinaryStream.SaveToFile sFILE, adSaveCreateOverWrite
  28.     oBinaryStream.Flush
  29.     oBinaryStream.Close
  30.     Set oUTF8Stream = Nothing
  31.     Set oBinaryStream = Nothing
  32. End Sub
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2