Search code examples
delphifiredac

How to append to an existing file using FireDAC


I would like to write to an existing .csv file not create a new one every time it runs. Here is the code I have so far.

with TFDBatchMoveDataSetReader.Create(FDBatchMove) do begin
      DataSet:= Inventory.mInventoryCount;
      Optimise:= False;
    end;
    with TFDBatchMoveTextWriter.Create(FDBatchMove) do
    AssignFile(myFile, 'C:\Dataout.csv');
    //FileName:= ExtractFilePath('C:\') + 'DataOut.csv';
    Append(myfile);
    FDBatchMove.Execute

Solution

  • You are not supposed to do writing by yourself. That's what the writer does for you. You need to just control the Mode property and the clear options of the Options option set of the TFDBatchMove to instruct the engine what the writer should do. From what you say it sounds like you are interested in the dmAlwaysInsert mode (which is default) and keeping poClearDest and poClearDestNoUndo options not included in the option set (which is default as well).

    Or in other words, FireDAC will append data with no checks to the destination by default, hence the problem you've described could have happen by modifying some of the mentioned settings (or, you just misidentified the issue).