Question
· Nov 19, 2018

Convert UTF-8 encoded error files to ANSI encoded files

Hi All,

I am converting UTF-8 encoded error files to ANSI files and for that I had written below piece of code. But I am not able to get ANSI encoded files.

Code Snippets:

Set tReadStatus = ""
Set eol = 0
Set (tRecord1,tRecord) = ""
Set stream=##class(%Stream.FileCharacter).%New()
Set sc=stream.LinkToFile(tErrorFileName)
Do stream.TranslateTableSet("cp1252")
Set tNewStream = ##class(%Stream.FileCharacter).%New()
Set tNewStream.Filename="D:\Bhupi\Test6.txt"
Do tNewStream.TranslateTableSet("cp1252")
While ('stream.AtEndGet())
{
Set tRecord=stream.ReadLine(,.tReadStatus,.eol)
$$$TRACE("Record1 :"_tRecord)
Do tNewStream.WriteLine(tRecord)

;Set tStatus =tNewStream.CopyFrom(stream)
Set tStatus=tNewStream.%Save()

When I checked Test6.txt file in Notepad++ it is created in UTF-8 format, although in ensemble trace it is displaying as ANSI encoded format which I believe because of TranslateTableSet property.

I had set same property in my target stream file as well but no luck.  Could anybody suggest anything moving forward.

Regards,

Bhupinder 

Discussion (2)1
Log in or sign up to continue

Thanks for the suggestion, File is now getting converted into ANSI encoded format but now I am not able to copy file from one directory to another.

When I  used CopyFile method It started working  but without the below mentioned line in code snippet no file is getting created. Because of this two files are getting created. Please have a look and suggest.

Set tOK=##Class(%File).CopyFile(tFileName,tErrorFileName)

Code Snippet()

Set tFileName = tFullFilename

$$$TRACE("tFileName [" _ tFileName _ "]")
Set tErrorFileName = ##class(Ens.Util.File).fixPath(..Adapter.ErrorPath) _ ##class(Ens.Util.File).CreateTimestamp(##class(%File).GetFilename(tFileName))
;Set tErrorFileName1 = ##class(Ens.Util.File).fixPath(..Adapter.ErrorPath) _ ##class(Ens.Util.File).CreateTimestamp(##class(%File).GetFilename(tErrorFileName))

$$$TRACE("tErrorFileName [" _ tErrorFileName1 _ "]")
Set tOK=##Class(%File).CopyFile(tFileName,tErrorFileName)

$$$TRACE("ErrorFile "_tErrorFileName_" written")
;Code2 Start Here===============================================================
Set tReadStatus = "" 
Set eol = 0
Set (tRecord1,tRecord) = ""
Set stream=##class(%Stream.FileCharacter).%New()
Set sc=stream.LinkToFile(tErrorFileName)
Do stream.TranslateTableSet("UTF8")
Set tNewStream = ##class(%Stream.FileCharacter).%New()
Set tNewStream.Filename=tErrorFileName1
Do tNewStream.TranslateTableSet("cp1252")
While ('stream.AtEndGet())

Set tRecord=stream.ReadLine(,.tReadStatus,.eol)
$$$TRACE("Record1 :"_tRecord)
Do tNewStream.WriteLine(tRecord)

;Set tStatus =tNewStream.CopyFrom(stream)
Set tStatus=tNewStream.%Save()
Quit:('tStatus)