none
How to tell if a file is locked

    Question

  • I have created an .hta for users to run to copy data from their hard drive to a share. I would like to be able to provide users with as much data as possible about why a file may have not copied. I would like to be able to tell a user that a file wasn’t copied because it was locked, but I can’t see how to determine if a file is locked or not.

     

    Currently I am using the file system object to copy files. I am capturing the error code from each copy to determine if the copy was successful or not. From everything I can see the error code will always be 0 or 1.

     

    I see that WMI can be used to copy files and returns one of several different error codes. It looks like a locked file should return an error code of 15, but in my testing locked files return the general error code of 8. Also, the WMI copy process doesn’t work with UNC paths.

    http://msdn.microsoft.com/en-us/library/aa389347(VS.85).aspx

     

    Any ideas on how I might be able to tell if the copy failed because the source was locked?

    Monday, January 11, 2010 7:47 PM

Answers

  • Typically, you can successfully copy files that are locked....so you may be researching a non-existent problem.....you need to test before you assume this is an issue.  Many times copy works just fine on locked files.

    As far as i know in VBScript (natively) you can assume a file is locked after you check permissions and attempt to delete the file.  Best to do both, but could just attempt the delete by itself.

    For example: if you receive an error attempting to copy then attemt a delete.  If the delete fails then the file is most likely locked....but it could be a permissions issue as well.  However, the delete could be an issue if the file can't be copied first.

    If you wanted to verify permissions then you would use VBscript to interogate the file permissions and check to see if the user has access.  The correct course of action for this would be to verify access before the copy, then if the copy fails, attempt to delete the file (or just assume the file is locked), and report the file as locked to the user.


    • Marked as answer by hoeyshane Friday, June 04, 2010 2:45 AM
    Monday, January 11, 2010 8:43 PM
  • Hi

    I am trying to clear up the open posts in this forum.

    If this is still an unresolved issue for you please let us know by replying to this thread.

    If it's resolved then it would be great if you could post the resolution so others can read the solution.

    If there's no activity in this thread during the next week, I'll assume it's resolved and will close this thread.

    Thanks
    Shane


    Shane Hoey psugbne.org | Powershell Usergroup Brisbane
    • Marked as answer by hoeyshane Friday, June 04, 2010 2:37 AM
    Friday, May 28, 2010 11:44 PM

All replies

  • Typically, you can successfully copy files that are locked....so you may be researching a non-existent problem.....you need to test before you assume this is an issue.  Many times copy works just fine on locked files.

    As far as i know in VBScript (natively) you can assume a file is locked after you check permissions and attempt to delete the file.  Best to do both, but could just attempt the delete by itself.

    For example: if you receive an error attempting to copy then attemt a delete.  If the delete fails then the file is most likely locked....but it could be a permissions issue as well.  However, the delete could be an issue if the file can't be copied first.

    If you wanted to verify permissions then you would use VBscript to interogate the file permissions and check to see if the user has access.  The correct course of action for this would be to verify access before the copy, then if the copy fails, attempt to delete the file (or just assume the file is locked), and report the file as locked to the user.


    • Marked as answer by hoeyshane Friday, June 04, 2010 2:45 AM
    Monday, January 11, 2010 8:43 PM
  • Outlook can lock personal folder files (.pst) so they will not be able to be copied.

    I really don't want to be making any changes to the user's data files on their hard drive. I don't want there to be a chance that a user can come back to me complaining that my process deleted any of their data.

    Monday, January 11, 2010 8:49 PM
  • Hi

    I am trying to clear up the open posts in this forum.

    If this is still an unresolved issue for you please let us know by replying to this thread.

    If it's resolved then it would be great if you could post the resolution so others can read the solution.

    If there's no activity in this thread during the next week, I'll assume it's resolved and will close this thread.

    Thanks
    Shane


    Shane Hoey psugbne.org | Powershell Usergroup Brisbane
    • Marked as answer by hoeyshane Friday, June 04, 2010 2:37 AM
    Friday, May 28, 2010 11:44 PM