Welcome to WebmasterWorld Guest from 18.204.227.250

Forum Moderators: open

Message Too Old, No Replies

Error trapping with FileSystemObject

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

New User

10+ Year Member

joined:Dec 3, 2003
posts:6
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 25, 2002
posts:3185
votes: 0


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

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

New User

10+ Year Member

joined:Dec 3, 2003
posts:6
votes: 0


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).

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

Senior Member

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 25, 2002
posts:3185
votes: 0


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.

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

New User

10+ Year Member

joined:Dec 3, 2003
posts:6
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 25, 2002
posts:3185
votes: 0


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...

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

Senior Member

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 25, 2002
posts:3185
votes: 0


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