Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for txt2utf8.vbs

(view source code of txt2utf8.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim objFSO, strFileIn, strFileOut
  4.  
  5. strFileIn = WScript.ScriptName
  6.  
  7. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  8. strFileOut = objFSO.GetBaseName( strFileIn ) & "_utf8.txt"
  9. Set objFSO = Nothing
  10.  
  11. UTF8 strFileIn, strFileOut
  12.  
  13.  
  14. Function UTF8( myFileIn, myFileOut )
  15. ' UTF8()  Version 1.00
  16. ' Open a "plain" text file and save it again in UTF-8 encoding
  17. ' (overwriting an existing file without asking for confirmation).
  18. '
  19. ' Based on a sample script from JTMar:
  20. ' http://bytes.com/groups/asp/52959-save-file-utf-8-format-asp-vbscript
  21. '
  22. ' Written by Rob van der Woude
  23. ' http://www.robvanderwoude.com
  24.  
  25. 	Dim objStream
  26.  
  27. 	' Valid Charset values for ADODB.Stream
  28. 	Const CdoBIG5        = "big5"
  29. 	Const CdoEUC_JP      = "euc-jp"
  30. 	Const CdoEUC_KR      = "euc-kr"
  31. 	Const CdoGB2312      = "gb2312"
  32. 	Const CdoISO_2022_JP = "iso-2022-jp"
  33. 	Const CdoISO_2022_KR = "iso-2022-kr"
  34. 	Const CdoISO_8859_1  = "iso-8859-1"
  35. 	Const CdoISO_8859_2  = "iso-8859-2"
  36. 	Const CdoISO_8859_3  = "iso-8859-3"
  37. 	Const CdoISO_8859_4  = "iso-8859-4"
  38. 	Const CdoISO_8859_5  = "iso-8859-5"
  39. 	Const CdoISO_8859_6  = "iso-8859-6"
  40. 	Const CdoISO_8859_7  = "iso-8859-7"
  41. 	Const CdoISO_8859_8  = "iso-8859-8"
  42. 	Const CdoISO_8859_9  = "iso-8859-9"
  43. 	Const cdoKOI8_R      = "koi8-r"
  44. 	Const cdoShift_JIS   = "shift-jis"
  45. 	Const CdoUS_ASCII    = "us-ascii"
  46. 	Const CdoUTF_7       = "utf-7"
  47. 	Const CdoUTF_8       = "utf-8"
  48.  
  49. 	' ADODB.Stream file I/O constants
  50. 	Const adTypeBinary          = 1
  51. 	Const adTypeText            = 2
  52. 	Const adSaveCreateNotExist  = 1
  53. 	Const adSaveCreateOverWrite = 2
  54.  
  55. 	On Error Resume Next
  56.  
  57. 	Set objStream = CreateObject( "ADODB.Stream" )
  58. 	objStream.Open
  59. 	objStream.Type = adTypeText
  60. 	objStream.Position = 0
  61. 	objStream.Charset = CdoUTF_8
  62. 	objStream.LoadFromFile myFileIn
  63. 	objStream.SaveToFile myFileOut, adSaveCreateOverWrite
  64. 	objStream.Close
  65. 	Set objStream = Nothing
  66.  
  67. 	If Err Then
  68. 		UTF8 = False
  69. 	Else
  70. 		UTF8 = True
  71. 	End If
  72.  
  73. 	On Error Goto 0
  74. End Function
  75.  

page last modified: 2024-04-16; loaded in 0.0061 seconds