While working on my new pet project, I encountered a situation in which I wanted to save some data to a file. From the Vista Sidebar Gadget point of view, you can do it pretty easy using Scripting.FileSystemObject.

As in any Microsoft product, you should ask about necessary credentials. So find out that inside the gadget’s directory, you are free to create, delete, update files with no more rights required than the ones that the gadget already owns.

As I searched for more details, I found out here that the correct usage was:

   1:  function saveXmlDoc(xmlDoc)
   2:  {
   3:      //get storage path
   4:      var path = System.Gadget.path 
   5:         + '\\Data\\exchange.xml';
   7:      //create/open text file
   8:      var fso = 
   9:         new ActiveXObject("Scripting.FileSystemObject");
  10:      var output = 
  11:         fso.OpenTextFile(path, ForAppending, true);
  13:      //write XML data to file
  14:      output.WriteLine(xmlDoc[0].xml);
  16:      //close file
  17:      output.Close();
  18:  }

The syntax of the OpenTextFile is :

   FSO.OpenTextFile(filename[, inputOuputMode[, createNewFile[, textFileFormat]]]), where:

  1. filename – the name of file to open
  2. inputOuputMode -you can use eiter number as parameter or the actual string representing the parameter, as follows:  1 or ForReading, 2 or ForWriting, 8 or ForAppending
  3. createNewFile – should be set ‘true’ if you want to create a new text file if filename doesn’t exist
  4. textFileFormat – can be one of 3 values: 0 – creates the textfile in ascii format; 1 – creates the textfile in unicode format; 2 – uses default system type.

The file can write in several ways:

  1. output.WriteLine – Write data to an open text file with a trailing newline character
  2. output.Write – Write data to an open text file with a trailing newline character.
  3. output.WriteBlankLines – Write one or more blank lines to an open text file:
   1:  // Write three newline characters to the file.        
   2:     output.WriteBlankLines(3) ;