NodeJs: Check if a file is locked without locking the file

Viewed 1362

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

1 Answers

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

Related