4 Replies Latest reply on Jun 28, 2017 2:43 AM by carlesc RSS
    bmessenger Newbie

    Append to CSV file with CSVParserFunctions WriteCSVFile


    Does anybody know how to append to a CSV file? If I use this


    var paramsCSV = {

      path: sFile  /* STRING */,

      data: infoTab /* INFOTABLE */,

      fileRepository: "P116SQLXMLFileRepository" /* THINGNAME */,

      withHeader: false/* BOOLEAN */




    // no return



    I always get a new file each time. I actually want to append to the file each time. There seems to be no option in the params to tell it to append.

      • Re: Append to CSV file with CSVParserFunctions WriteCSVFile
        bmessenger Newbie


        I'll answer my own question because I have found a way around it. If I read the data from the existing csv file into an existing datashape, then append the new data to this datashape and then write the resulting datashape out to file it works as an append. This does seem a rather long way to do things considering every language has the ability to append to a file.


        SO this is what I have done in the code


        //Note I have some XML incoming residing in xmlPage varaible and it is to be written to a CSV file aftedr parsing the XML


        //Get an existing file

        var sFile = "P116ProdLossesFromSQL.csv";


        //Fill the datashape GetSQLProdLossData with data from the existing CSV file

        var params4 = {

                    path: sFile /* STRING */,

                    columnMappings: undefined /* STRING */,

                    hasHeader: true /* BOOLEAN */,

                    longitudeField: undefined /* NUMBER */,

                    dateFormat: "yyyy-MM-dd HH:mm:ss" /* STRING */,

                    fileRepository: "P116SQLXMLFileRepository" /* THINGNAME */,

                    latitudeField: undefined /* NUMBER */,

                    fieldDelimiter: undefined /* STRING */,

                    stringDelimiter: undefined /* STRING */,

                    dataShape: "GetSQLProdLossData" /* DATASHAPENAME */



        // result: INFOTABLE

        var result = Resources["CSVParserFunctions"].ReadCSVFile(params4);


        var xmlPage = me.XMLFileRepoString; ??This is XML that sits in an XML property of the thing


        //Append the XML data that has come in to the datashape GetSQLProdLossData

        var i = 0;

        for each (var rT in xmlPage.tT.rT)


            logger.debug("got debug to row " + i);




            var dateValue = parseDate(rT.D, "yyyy-MM-dd'T'HH:mm:ss");


            // GetSQLProdLossData entry object

            var newEntry = new Object();

            newEntry.DateAndTime = dateValue; // DATETIME

            newEntry.Val = parseInt(rT.V,10); // INT

            newEntry.Plant = rT.P; // STRING

            newEntry.Millitm = parseInt(rT.M,10); // INT

            newEntry.TagIndex = parseInt(rT.T,10); // INT

            if(rT.S != '')

            newEntry.Status = parseInt(rT.S,10); // INT


                newEntry.Status = -1;

            newEntry.Marker = rT.N; // STRING




        // result: INFOTABLE dataShape: "GetSQLProdLossData"

        var infoTab = result;


        var paramsCSV = {

          path: sFile  /* STRING */,

          data: infoTab /* INFOTABLE */,

          fileRepository: "P116SQLXMLFileRepository" /* THINGNAME */,

          withHeader: true /* BOOLEAN */




        // no return