Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for gettxttype.vbs

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

  1. Option Explicit
  2.  
  3. Dim objFile, objFSO
  4. Dim strEnc, strFile, strTest
  5.  
  6. Const ForReading    = 1
  7. Const TristateFalse = 0
  8.  
  9.  
  10. With WScript.Arguments
  11. 	If .Named.Count > 0 Then
  12. 		Syntax
  13. 	End If
  14. 	If .Unnamed.Count = 1 Then
  15. 		strFile = WScript.Arguments(0)
  16. 		Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  17. 		If objFSO.FileExists( strFile ) Then
  18. 			Set objFile = objFSO.OpenTextFile( strFile, ForReading, TristateFalse )
  19. 		Else
  20. 			Set objFSO = Nothing
  21. 			Syntax
  22. 		End If
  23. 	Else
  24. 		Syntax
  25. 	End If
  26. End With
  27.  
  28. strEnc = "ANSI"
  29.  
  30. strTest = objFile.Read( 4 )
  31.  
  32. If strTest = Chr(   0 ) & Chr(   0 ) & Chr( 254 ) & Chr( 255 ) Then strEnc = "Unicode-32 (BE)"
  33. If strTest = Chr(   0 ) & Chr(   0 ) & Chr( 255 ) & Chr( 254 ) Then strEnc = "Unicode-32 (LE)"
  34. If strTest = Chr(  43 ) & Chr(  47 ) & Chr( 118 ) & Chr(  43 ) Then strEnc = "UTF-7"
  35. If strTest = Chr(  43 ) & Chr(  47 ) & Chr( 118 ) & Chr(  47 ) Then strEnc = "UTF-7"
  36. If strTest = Chr(  43 ) & Chr(  47 ) & Chr( 118 ) & Chr(  56 ) Then strEnc = "UTF-7"
  37. If strTest = Chr(  43 ) & Chr(  47 ) & Chr( 118 ) & Chr(  57 ) Then strEnc = "UTF-7"
  38. If strTest = Chr( 221 ) & Chr( 115 ) & Chr( 102 ) & Chr( 115 ) Then strEnc = "UTF-EBCDIC"
  39. If strTest = Chr( 132 ) & Chr(  49 ) & Chr( 149 ) & Chr(  51 ) Then strEnc = "GB-18030"
  40.  
  41. strTest = Left( strTest, 3 )
  42.  
  43. If strTest = Chr( 239 ) & Chr( 187 ) & Chr( 191 ) Then strEnc = "UTF-8"
  44. If strTest = Chr( 247 ) & Chr( 100 ) & Chr(  76 ) Then strEnc = "UTF-1"
  45. If strTest = Chr(  14 ) & Chr( 254 ) & Chr( 255 ) Then strEnc = "SCSU"
  46. If strTest = Chr( 251 ) & Chr( 238 ) & Chr(  40 ) Then strEnc = "BOCU-1"
  47.  
  48. strTest = Left( strTest, 2 )
  49.  
  50. If strTest = Chr( 254 ) & Chr( 255 ) Then strEnc = "Unicode-16 (BE)"
  51. If strTest = Chr( 255 ) & Chr( 254 ) Then strEnc = "Unicode-16 (LE)"
  52.  
  53. WScript.Echo strEnc
  54.  
  55. objFile.Close
  56. Set objFile = Nothing
  57. Set objFSO  = Nothing
  58.  
  59.  
  60. Sub Syntax( )
  61. 	Dim strMsg
  62. 	strMsg = vbCrLf _
  63. 	       & "GetTxtType.vbs,  Version 1.00" & vbCrLf _
  64. 	       & "Find a text file's text encoding by reading its BOM (Byte Order Mark)" _
  65. 	       & vbCrLf & vbCrLf _
  66. 	       & "Usage:  GETTXTTYPE.VBS  input_file" _
  67. 	       & vbCrLf & vbCrLf _
  68. 	       & "Note:   If no BOM is found, the script assumes ANSI." _
  69. 	       & vbCrLf & vbCrLf _
  70. 	       & "Written by Rob van der Woude" & vbCrLf _
  71. 	       & "http://www.robvanderwoude.com" & vbCrLf
  72. 	WScript.Echo strMsg
  73. 	WScript.Quit 1
  74. End Sub
  75.  

page last uploaded: 2016-12-15, 11:20