Forum Moderators: open

Message Too Old, No Replies

Error trapping with FileSystemObject

         

leeshanokb

4:04 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



I'm not entirely sure if this falls under the realm of Javascript, but I'm using JS to open a file, and I was wondering what (if anything) I can do to trap an error that occurred within an ActiveX Control.

For example, let's say the file c:\output.ini was locked.. how would I trap an error within the following code?

<script type="text/javascript">
var fso, f1, ts, content
var ForReading = 1
var ForWriting= 2;
var ForAppending=8;
fso = new ActiveXObject("Scripting.FileSystemObject")
f1 = fso.OpenTextFile("c:\\output.ini", ForWriting);
content = f1.WriteLine("Test")
f1.close()

</script>

Thanks,
Bruce

BlobFisk

4:30 pm on Dec 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi leeshanokb,

The code that you have posted above looks like ASP (written in VBScript) rather than JavaScript.

JavaScript is not able to read or write to files as it is a client-side script. You need a server-side script to do this, and the code you have posted above looks like this.

HTH

leeshanokb

4:55 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



Hmm. I'm a bit confused I guess, I'm new at this so sorry if these questions seem asinine. This code is actually running locally.. and currently its running/writing (I suppose because the rules only apply to remote files?). I'm just concerned about how to trap an error with the file writing, regardless of how the file.

I'm not even sure what ASP is. :) But maybe I am using it (sad isn't it, when I'm not even sure what language I'm writing in?).. up until recently I'm fairly certain it was Javascript. :) Should I be doing something differently, with regards to what I'm writing?

... but as bad as this sounds, in the end I don't think I really care what language I'm writing in. I just want to be able to trap errors (specifically writing to open files).

BlobFisk

5:03 pm on Dec 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK - on second look, the code seems like JavaScript, my apologies for the confusion!

However, while this works locally, it should not work on the web, as it is trying to open a local file on the users machine. Security prevents a client-side script from accessing files on the client machine.

You need to look at server side scripting to read from/write to files on the web server.

leeshanokb

6:32 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



Okay, phew. You had me worried there for a second. ;)

I guess that's what I'm getting at, the intention is to just have it run locally. This script will never run anywhere other than on a local machine. But what I'm concerned about is either:

a.) The file not writing properly because it's already opened.
b.) An error message appearing when writing to/opening a file because the file is already opened.

and I was looking for a way to trap that error, given that I'm running this entire thing locally.

Cheers,
Bruce

BlobFisk

11:23 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm... I'm not too sure - but I doubt it. That sort of communication would come from the ActiveX object.

You may be better off working with a Java Applet here...

BlobFisk

11:34 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is another thread on JavaScript and file access here [webmasterworld.com] in the Javascript forum...