2

I am trying to check if a file has been locked with another application.

I figured than I can do that easily by opening the file in writing mode.

fs.open('myfile.docx', 'r+', (err) => {
  if (!err) {
    console.log('File unlocked')
  }
})

However, I do not want to lock the file because the other app could write data at any moment.

So, is there any way to check if a file is locked without locking it (and without administrative rights by using either node.js or windows cmd) ?

Yenyen
  • 21
  • 4
  • I think here is the answer: https://stackoverflow.com/questions/37690321/node-js-test-to-see-if-a-file-is-locked-for-editing-by-another-process#37707620 – lependu Nov 13 '18 at 16:50
  • @lependu thanks, unfortunately, in the answer he locks the file. We are trying to avoid that. – Yenyen Nov 13 '18 at 17:11

1 Answers1

0

You can use the Handle utility from Sysinternals.

It works by looping through all Windows processes and listing their open file handles. You may want to filter the output:

handle.exe | findstr myfile.docx

You can invoke this from node using child_process.exec

mihai
  • 37,072
  • 9
  • 60
  • 86
  • Thank you for this solution, but handle seams to need administrative rights and my app has only user rights. – Yenyen Nov 14 '18 at 07:35