Saturday, October 18, 2008

File operations using java script?

IE by microsoft has provided an activex object for this file operations (Scripting.FileSystemObject).
Synrax:-
var obj=new ActiveXObject("Scripting.FileSystemObject");

This object obj can be used to read/write/append data to text file.
Example:-
var fso=new ActiveXObject("Scripting.FileSystemObject");
var file= fso.CreateTextFile(FilePath,true);
file.writeline('Hai');
file.close();

This creates a text file on the specific file path mentioned and writes data("Hai").

var fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(FilePath)
{
var file = fso.OpenTextFile(FilePath,8,false);
file.writeline('Friends');
file.close();
}

This appends text to an existing text file, where (8) says to append text to an existing file.

if(fso.FileExists(FilePath))
{
var file=fso.OpenTextFile(FilePath,1,false);
alert(file.readAll());
file.close();
}

This reads content from the text file and displays an alert box, where (1) indicates to read content from file.

No comments: