Automating QTP Test Automation Home Automation Articles Downloads QTP Gotchas Links Books Contact About Site Map
File Function - Get EncodingThis function determines the encoding of a file, Unicode, Utf-8 or Ansi/Ascii. This is important because if you open a Unicode file in Ansi mode (the default), when you try to read a line it will be complete gibberish. QTP function files (.qfl) are saved in Unicode so if you want to start editing them programmatically you need to know this. It works by reading the first byte (in ansi mode), for Unicode it's 255, for Utf-8 it's 239. If you find an error, or have a suggestion for improvement, Email me and I'll fix it. Download it Here (right-click and then save target as). (NOTE - you can just cut & paste from below, but the formatting will have multiple spaces instead of tabs).
'=========================================================================
Function zFile_GetEncoding(sFileName) Dim oFso, oFile, iChar Const ForReading = 1 Set oFso = CreateObject("Scripting.FileSystemObject") 'Open the file for reading - use ascii (default) Set oFile = oFso.openTextFile(sFileName, ForReading) 'Get the first byte iChar = Asc(oFile.Read(1)) oFile.Close Set oFile = Nothing If iChar = 255 Then zFile_GetEncoding = "Unicode" ElseIf iChar = 254 Then zFile_GetEncoding = "UniBig" 'Unicode Big Endian ElseIf iChar = 239 Then zFile_GetEncoding = "Utf8" Else zFile_GetEncoding = "Ansi" End If End Function '========================================================================= |