Author Topic: Increment Counter using Appendfile  (Read 2353 times)

Benjamin Dasari

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 95
    • View Profile
Increment Counter using Appendfile
« on: February 16, 2016, 11:47:29 AM »
Purpose:
To use appendfile command and increment counter in a file. This could also be used as an example to use appendfile to add to a file.

Liquid UI Code:

function testFunction() {
   strFileName = 'counter.txt';
   appendToCounterFile(strFileName);
}

// Toolbar pushbutton on User interface screen
pushbutton([TOOLBAR],'Execute','?',{'process':testFunction});

// Function is called to remove blank spaces
String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
}

// Function is called to validate if the variable holds blank or null value
function isBlank(jvar){
   if(typeof jvar == undefined || jvar == null || jvar == "" || jvar == void 0)
      return true;
   else
      return jvar.toString().trim() == '';
}

// Function which takes as parameter the file and appends to it
function appendToCounterFile(fileName) {
   var lastCounter = 0;
   var z_counter = '';
   var strBlank = '';
   openfile(fileName, {"delimiter":","});
   NEXT_LINE_READ:;
   readfile(fileName, {"z_counter":true});
   if(isBlank(z_counter)) {
      goto END_OF_FILE;
   }
   lastCounter = parseInt(z_counter);
   var z_counter = '';
   goto NEXT_LINE_READ;

   END_OF_FILE:;
   closefile(fileName);
   
   openfile(fileName, {"append":true, "delimiter":","});
   lastCounter++;
   appendfile(fileName, {"lastCounter":true});
   closefile(fileName);
}


See attachments for code samples!
« Last Edit: February 19, 2016, 07:22:19 PM by Benjamin Dasari »