2 Replies - 4127 Views - Last Post: 27 June 2012 - 08:23 AM Rate Topic: -----

#1 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

VBScript: Test if a file is unicode

Posted 27 June 2012 - 05:56 AM

So, I have a project where I'm reading in files using a variant of VBScript/VBA. I need to find out of a text file I'm reading in is in ANSI ASCII format or Unicode format. I have a small snippet that can be modified to read/write ANSI ASCII or Unicode files. I'm kind of stuck on determining the format of a text file so I can use the appropriate arguments when opening the file. Is it the first few bytes of the file that determine if a file is an ANSI ASCII file or a Unicode file? A reference to an article or any advice would be appreciated.

Option Explicit

Const ForReading = 1			' Opens the file for reading
Const ForWriting = 2			' Opens the file for writing
Const ForAppending = 8		' Opens the file for appending

Const TristateUseDefault = -2	' Opens the file using the system default
Const TristateTrue = -1		' Opens the file as Unicode
Const TristateFalse = 0		' Opens the file As ASCII

Sub Main
	WriteFile
	ReadFile
End Sub

Sub WriteFile
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set a = fso.CreateTextFile("testfile.txt", true, true)
	a.WriteLine("This is a test.")
	'a.WriteLine("The quick brown fox jumped over the lazy dog.")
	a.Close
End Sub

Sub ReadFile
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set a = fso.OpenTextFile("testfile.txt", ForReading, false, TristateTrue)
	Do While Not a.AtEndOfStream
		a.Read(1)
	Loop
	a.Close
End Sub

This post has been edited by SixOfEleven: 27 June 2012 - 06:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: VBScript: Test if a file is unicode

#2 denting5  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: VBScript: Test if a file is unicode

Posted 27 June 2012 - 07:34 AM

This method is easy but non-technical. Paste in a unicode special character, and see if it renders properly. <--- use that.
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: VBScript: Test if a file is unicode

Posted 27 June 2012 - 08:23 AM

That is not very helpful when trying to read in a file and you don't know what format it is in. I did however find a solution that works OK and will be good for my situation. I used the BOM of unicode files to test if a file is unicode or not, from this article.


Option Explicit

Const AscII = 0
Const Unicode = 1

Const ForReading = 1			' Opens the file for reading
Const ForWriting = 2			' Opens the file for writing
Const ForAppending = 8		' Opens the file for appending

Const TristateUseDefault = -2	' Opens the file using the system default
Const TristateTrue = -1		' Opens the file as Unicode
Const TristateFalse = 0		' Opens the file As ASCII

Sub Main
	Dim fileMode As Boolean
	Dim fileType As Integer
	
	WriteFile "D:\Test\testfile2.txt", true

	If (TestFile("D:\Test\testfile2.txt") = Unicode) Then
		fileMode = true
		fileType = TristateTrue
	Else
		fileMode = false
		fileType = TristateFalse
	End If
	
	ReadFile "D:\Test\testfile2.txt", fileType
End Sub

Function TestFile(ByVal path As String) As Integer
	Dim buffer As String
	Dim InFileNum As Integer
	Dim firstByte As Integer
	Dim secondByte As Integer
	Dim thirdByte As Integer
	
	buffer = String(100, " ")
	
	InFileNum = FreeFile
	
	Open path For Binary Access Read As InFileNum

	Get InFileNum, 0, buffer

	Close InFileNum

	firstByte = Asc(Mid(buffer, 1, 1))
	secondByte = Asc(Mid(buffer, 2, 1))
	thirdByte = Asc(Mid(buffer, 3, 1))
		
	If (firstByte = 255 And secondByte = 254) Then
		TestFile = Unicode
	ElseIf (firstByte = 254 And secondByte = 255) Then
		TestFile = Unicode
	ElseIf (firstByte = 239 And secondByte = 187 And thirdByte = 191) Then
		TestFile = Unicode
	Else
		TestFile = AscII
	End If
	 
End Function

Sub WriteFile(ByVal path As String, ByVal mode As Boolean)
	Dim fso As Object
	Dim a As Object
	
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set a = fso.CreateTextFile(path, true, mode)
	a.WriteLine("This is a test.")
	a.WriteLine("The quick brown fox jumped over the lazy dog.")
	a.Close
	
	Set a = Nothing
	Set fso = Nothing
End Sub

Sub ReadFile(ByVal path As String, ByVal mode As Integer)
	Dim fso As Object
	Dim a As Object
	
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set a = fso.OpenTextFile(path, ForReading, false, mode)
	Do While Not a.AtEndOfStream
		MsgBox a.ReadLine
	Loop
	a.Close
	
	Set a = Nothing
	Set fso = Nothing
End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1