Highlighted
Absent Member.. Absent Member..
Absent Member..
1459 views

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?

4 Replies
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: write file

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

Highlighted
Contributor.
Contributor.

Re: write file

Hi ,

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


Any help is greatly appreciated.

Thanks in advance.

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: write file

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();

Highlighted
Frequent Visitor.

Re: write file

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!

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.