1

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 );
5
  • 2
    You can't do file access in javascript, can you? Commented Jun 30, 2009 at 20:47
  • 2
    What kind of environment? In a browser? Running as a script on something else? Via the CLR/.NET? Commented Jun 30, 2009 at 20:48
  • 2
    @skaffman, yes, but you have to use Java to do it. Commented Jun 30, 2009 at 20:50
  • Are you using something like this- msdn.microsoft.com/en-us/library/314cz14s(VS.85).aspx? Commented Jun 30, 2009 at 20:51
  • It's using var xmlDoc = new ActiveXObject( "Msxml2.DOMDocument.3.0" ). xmlDoc.load( strXMLpath ) Commented Jun 30, 2009 at 20:55

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

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

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.