2

The code I'm writing in Java is is close a file left open by the user. So, here is what typically happens: a user is editing an Excel file, they save it, leave it open, and then close the lid on their laptop. The file is still kept open and locked so no one else can edit it. Is there a way to kick them off and unlock the file? When they are using the file, it is "checked out." Here is what shows up:

What checked out looks like: (image)

The following code, interfacing through WinDAV with SharePoint, tells me if a file is locked or not (I know it's not great code, but it works and I've tried several other solutions including Filelock, Apache IO, FileStream, etc.):

String fileName = String.valueOf(node);
        File file = new File(fileName);
        boolean replaced;

        File sameFileName = new File(fileName);

        if(file.renameTo(new File(sameFileName + "_UNLOCK"))){
            replaced = true; //file is currently not in use
            (new File(sameFileName + "_UNLOCK")).renameTo(sameFileName);
        }else{
            replaced = false; //file is currently in use
        }

So, how would I unlock a file now? The only other solution is PowerShell using SharePoint libraries, but that has a whole lot of other problems...

halfer
  • 19,824
  • 17
  • 99
  • 186
  • you need to identify the process that locked the file and kill that process. for finding the process i only know some gui options (like process explorer from sysinternals), you can look for any cli options which will do the same. – Jos Jun 15 '17 at 18:34
  • I'll look into that. I added an image, so do you think you can look at that and tell me what you think? The file is locked by SharePoint, so I can't really kill SharePoint unless I want everyone to complain. Would there be any other way? Like create a copy of the file and delete the in-use one? – Simmon Thind Jun 15 '17 at 18:38

1 Answers1

1

As per the post, you can use the tool Handle, which is a CLI tool to find out which process is locking the file. Once you have the process ID, you can kill that process. I'm not aware of any Java API that would identify the culprit process. For killing the process you can use taskkill, and you can call it using Runtime like this. Both the operation require you app to run at Administrator or above privilege.

Jos
  • 2,015
  • 1
  • 17
  • 22
  • Thank you! Okay, so if I run Handle and get the process, do you know of a way in Java to kill said process? – Simmon Thind Jun 15 '17 at 18:40
  • you could do something like the one mentioned in this post https://stackoverflow.com/a/11366828/3981536 – Jos Jun 15 '17 at 18:42
  • 1
    Okay, I'll get right on it! Thank you so much, RedFlar3! If this doesn't work, I also have a (admittedly very messy) way of doing it and hopefully you can provide your opinion? If a file is locked, I can duplicate the file and delete the original. The duplicate has the lock lifted on it, and has all the same parameters as well. And it is also simple to do. What do you think of that method if the Handle approach doesn't work? – Simmon Thind Jun 15 '17 at 18:50