views:

254

answers:

4

How can I check to see if a file is already open by another user in javascript? As it is right now, the program I'm trying to fix will open/edit a file then fail on trying to save if the file is already in use.

Also, is there an easy way to add a lock on the file so another process knows it's in use?

Edit: the program is a .hta using Active X Objects.

i guess i should have been more specific, here's some code about how it is opening/editing/saving the files.

var FileSystem = new ActiveXObject( "Scripting.FileSystemObject" );
var xmlDoc = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
var fFile = FileSystem.GetFile( strPath );
xmlDoc.load( fFile.Path );
// some method's to edit documentElement in xmlDoc...
xmlDoc.save( fFile.Path );
+3  A: 

Are you sure it's just JavaScript and not a combo of maybe an ActiveX or flash component? Is the file on the client or server? If server, this question makes more sense to me (ie. using some AJAX solution).

Detect
+1  A: 

You would probably need a server side locking feature. The javascript would call the server's 'save' script, which would return either a 'successful' status, or 'file locked'.

The simplest lock method that most programs use is creating another file with the same name but an extension such as '.lock'. A process checks if the file exists when opening the original, if so the file is in use and can only be opened as read only. If not, the lock file is created and the original can be edited.

GApple
A: 

will open/edit a file then fail on trying to save.

Javascript cannot open files or save them.
That may be your problem.

It could "edit" them - you can use JS to manipulate or edit an HTML page. [Even running a whole Rich Text Editor.]

But you then have to pass the page back to some other script to actually save those changes.

This is actually not true if you have Aptana or similar server side Javascript, or if it is being used [mozdev] to pass data to SQLite which can save its own data. If this is your case you should specify, as it is hardly typical Javascript usage.

SamGoody
+1  A: 

I'm not too familiar with ActiveX, but maybe when you open a file you could create a temporary file like file.ext.lock (and delete it when you save the file), so when another user tries to open the same file and sees the .lock file exists, you know it's being used.

Detect
this works, but I'm having trouble unlocking/deleting the file when the user opens a file, then closes the entire program instead of closing the file first.
jb