write file

Hello, expert.

 

What I want is make a log file from JavaScript.

I know writeFile() is exist. But it overwtite the file(not append).

 

How to do it?

  • You have to read the complete file (readFile), than append your new logentry and than write the file back.

  • Hi ,

    How writeFile can be used ?  I can't find any help regarding this JavaScript function.


    Any help is greatly appreciated.

    Thanks in advance.

  • Here is an example of how we use it:

     

    function writeToFile( path, binary, object )
    {

    //print( "Writing " path " to file..." );

    var output = writeFile( path, binary, object );

    //print( "The number of bytes written to file was: " output );

    return output;
    }

    function writeLocations()
    {

    filePath = "/download/ITAM/SM7locations.txt"; //Path on Server
    isBinary = null;

    var temp_arr = "";

    var locs = new SCFile("location");

    var theXMLDate = new XMLDate(new Date());
    var todaysDate = theXMLDate.getSCDateTimeString();
    var query = "sysmodtime>'" todaysDate "' - '2 00:00:00'";
    //var query = "true";

    rc1 = locs.doSelect(query);

    if (rc1 == RC_SUCCESS ) {
    do {
    temp_arr =locs.location;
    temp_arr ="\n";
    }
    while (locs.getNext() == RC_SUCCESS);
    }

    fileObject = temp_arr;

    writeToFile( filePath, isBinary, fileObject );

    }

    writeLocations();

  • Well hello,

    This might have changed since 2016 but, the function definition allows you to append to a file (now).

    writeFile( path, mode, object );

    "mode" is either "b" for binary, "" for text.
       As well, you can append an "a" for appending to an existing file.

    Ex.: writeFile( "C:\\temp\\oldfile.txt", "a", str1 );

    This would append a string "str1" to an existing, or new, file "C:\temp\oldfile.txt", in text mode.

    I am trying to find out how to specify the text encoding, which I am yet to find.  In text mode, it's UTF-8, and in binary, a text file is encoded as UCS-2 Little Endian.

    If I can't find out how to change this, the later will be my way to create a CSV files for Excel to recognize, as with accentuated letters (éà), Excel does not play well with UTF-8 encoding so far.

    Well I hope this first post on the forum helps someone!