How to save an ANSI file from Business Central?

Hi experts,

This is regarding Business Central cloud.

When I make a file, I can set the encoding to Windows, UTF-8, UTF-16 and MSDOS.

Our customer need a file with ANSI encoding.

How do I do that?

Hope, you can help.

Morten

Best Answer

  • MortenSteengaard
    MortenSteengaard Member Posts: 144
    Answer ✓
    Hi SanderDk and vaprog,

    Thank you for your reply.

    Since Microsoft don't support making an ANSI encoded file (with our local, Danish, letters), I have made it myself (with a little help from my friend, Erik Hougaard on YouTube).

    This way it is working fine and our customer does not have to pay for the Azure functions.

    I saw how Erik Hougaard made an app that created a WAVE file and I used that to make the code below. I know it does not support all letters and signs, but this is what the customer is willing to pay for - and it is all they need.

    procedure SaveAnsiEncodedTextFileFromTempBlob(var TempBlobLocal: Codeunit "Temp Blob"; FileName: Text)
    var
        InStream, IStream : InStream;
        OutStream: OutStream;
        TempBlobLocal2: Codeunit "Temp Blob";
        TextToWrite: Text;
        i: Integer;
        Byte1, Byte2, Byte3, Byte4, Byte5, Byte6 : Byte;
    begin
        // Read text from TempBlob
        TempBlobLocal.CreateInStream(InStream, TextEncoding::UTF8);
        InStream.ReadText(TextToWrite);

        TempBlobLocal2.CreateOutStream(OutStream);

        Byte1 := 230; // æ
        Byte2 := 248; // ø
        Byte3 := 229; // å
        Byte4 := 198; // Æ
        Byte5 := 216; // Ø
        Byte6 := 197; // Å

        for i := 1 to StrLen(TextToWrite) do begin
            if TextToWrite = 'æ' then
                OutStream.Write(Byte1)
            else if TextToWrite = 'ø' then
                OutStream.Write(Byte2)
            else if TextToWrite = 'å' then
                OutStream.Write(Byte3)
            else if TextToWrite = 'Æ' then
                OutStream.Write(Byte4)
            else if TextToWrite = 'Ø' then
                OutStream.Write(Byte5)
            else if TextToWrite = 'Å' then
                OutStream.Write(Byte6)
            else
                OutStream.Write(TextToWrite);
        end;

        // Save the file
        TempBlobLocal2.CreateInStream(IStream);

        DownloadFromStream(IStream, '', '', '', FileName);
    end;

Answers

  • SanderDk
    SanderDk Member Posts: 507
    For help, do not use PM, use forum instead, perhaps other people have the same question, or better answers.
  • MortenSteengaard
    MortenSteengaard Member Posts: 144
    Hi SanderDk,
    Thank you for your reply.
    You are right, but when I save the file on my local computer, open it in Notepad and select "Save as", then I can see that it is "UTF-8".
    The file must be imported into "EG Lønservice" and the letters æ, ø and å are not correct after the import.
    If I manually make a text file with ANSI encoding using Notepad, then it works fine.
    Best regards,
    Morten
  • vaprog
    vaprog Member Posts: 1,169
    So, how do you create the file? How do you tell the system to create the file with Windows encoding?
  • MortenSteengaard
    MortenSteengaard Member Posts: 144
    Hi vaprog,
    I have the data in a Temp Blob and save it to a file like this:

    TempBlob.CreateInStream(IStream, TEXTENCODING::Windows);
    //TempBlob.CreateInStream(IStream, TEXTENCODING::UTF8);
    //TempBlob.CreateInStream(IStream, TEXTENCODING::MSDos);

    Filename := 'Test.txt';
    DownloadFromStream(IStream, '', '', '', Filename);
  • vaprog
    vaprog Member Posts: 1,169
    I would expect what you do to work correctly. If it does not, I'd write to a tempfile, then download that tempfile. This way you can set the TEXTENCODING on the outstream.
  • MortenSteengaard
    MortenSteengaard Member Posts: 144
    Answer ✓
    Hi SanderDk and vaprog,

    Thank you for your reply.

    Since Microsoft don't support making an ANSI encoded file (with our local, Danish, letters), I have made it myself (with a little help from my friend, Erik Hougaard on YouTube).

    This way it is working fine and our customer does not have to pay for the Azure functions.

    I saw how Erik Hougaard made an app that created a WAVE file and I used that to make the code below. I know it does not support all letters and signs, but this is what the customer is willing to pay for - and it is all they need.

    procedure SaveAnsiEncodedTextFileFromTempBlob(var TempBlobLocal: Codeunit "Temp Blob"; FileName: Text)
    var
        InStream, IStream : InStream;
        OutStream: OutStream;
        TempBlobLocal2: Codeunit "Temp Blob";
        TextToWrite: Text;
        i: Integer;
        Byte1, Byte2, Byte3, Byte4, Byte5, Byte6 : Byte;
    begin
        // Read text from TempBlob
        TempBlobLocal.CreateInStream(InStream, TextEncoding::UTF8);
        InStream.ReadText(TextToWrite);

        TempBlobLocal2.CreateOutStream(OutStream);

        Byte1 := 230; // æ
        Byte2 := 248; // ø
        Byte3 := 229; // å
        Byte4 := 198; // Æ
        Byte5 := 216; // Ø
        Byte6 := 197; // Å

        for i := 1 to StrLen(TextToWrite) do begin
            if TextToWrite = 'æ' then
                OutStream.Write(Byte1)
            else if TextToWrite = 'ø' then
                OutStream.Write(Byte2)
            else if TextToWrite = 'å' then
                OutStream.Write(Byte3)
            else if TextToWrite = 'Æ' then
                OutStream.Write(Byte4)
            else if TextToWrite = 'Ø' then
                OutStream.Write(Byte5)
            else if TextToWrite = 'Å' then
                OutStream.Write(Byte6)
            else
                OutStream.Write(TextToWrite);
        end;

        // Save the file
        TempBlobLocal2.CreateInStream(IStream);

        DownloadFromStream(IStream, '', '', '', FileName);
    end;
  • Miklos_Hollender
    Miklos_Hollender Member Posts: 1,612

    Hello,

    My usual solution for many such problems, like encodings or for example Windows/Linux files and so on is the following:

    1. save the file to a given folder which is NOT the desired folder
    2. Use AI to generate a simple PowerShell script for the necessary conversions, adapt as necessary. After conversion it moves the file to the desired folder
    3. Schedule the PowerShell script to run every 5 minutes via Windows Task Scheduler. If more often say every 5 seconds is necessary, run it once and use a timed loop inside powershell.
    4. If you are worried the file is big and will be locked through writing, do this: once you have exported the file, also export an empty file with the same name and the extension .rdy and adapt the script to only process the file if it sees the .rdy file