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)
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
16: //close file
The syntax of the OpenTextFile is :
FSO.OpenTextFile(filename[, inputOuputMode[, createNewFile[, textFileFormat]]]), where:
- filename – the name of file to open
- 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
- createNewFile – should be set ‘true’ if you want to create a new text file if filename doesn’t exist
- 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:
- output.WriteLine – Write data to an open text file with a trailing newline character
- output.Write – Write data to an open text file with a trailing newline character.
- 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) ;