mortynbras has asked for the wisdom of the Perl Monks concerning the following question:

Hi. How can i check the "Modify" property of the file on windows. I know that with -r, -w and -x I can check whether my user can read, write and execute the file but it appears that on windows to delete the file i need to be able to modify it (even if I can read and write the file i may not be able to delete it).
  • Comment on Checking "Modify" property of the file on windows

Replies are listed 'Best First'.
Re: Checking "Modify" property of the file on windows
by BrowserUk (Patriarch) on Jul 03, 2016 at 12:33 UTC
    it appears that on windows to delete the file i need to be able to modify it (even if I can read and write the file i may not be able to delete it).

    Correct! It can also be more complex than that.

    The simplest way to get the full DACLs for a file or directory is:

    $dir = 'users'; $rights = `icacls $dir`; print $rights;; users NT AUTHORITY\SYSTEM:(OI)(CI)(F) BUILTIN\Administrators:(OI)(CI)(F) BUILTIN\Users:(RX) BUILTIN\Users:(OI)(CI)(IO)(GR,GE) Everyone:(RX) Everyone:(OI)(CI)(IO)(GR,GE) Successfully processed 1 files; Failed processing 0 files

    See the help for the icacls command for the explanation of the output

    Its not the friendliest format for the information, but it is easily parsed for specific purposes.

    Alternatively, take a look at jenda's Win32::FileSecurity. Be warned, it has limitations, lacks documentation and isn't the simplest of interfaces.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
Re: Checking "Modify" property of the file on windows
by Marshall (Canon) on Jul 03, 2016 at 10:42 UTC
    Can you post some code demonstrating how you can be able to write a file, but yet not be able to delete it? There is no -x permission on an NTFS file. There is such a thing as a "read only" file, but that is not what you are describing.

    One reason why you may not be able to delete a file, is that it is currently is use. There are solutions to that. Is that the case?

    Update: To "delete" a file that is currently in use, rename it to something else and then delete that file name. Once a file is "opened" and the program has a file handle, that's all that matters. You can prevent new users from opening that file using the "name", but you cannot not stop folks who already have it "open" from continuing to use it with the filehandle.

Re: Checking "Modify" property of the file on windows
by soonix (Chancellor) on Jul 03, 2016 at 21:39 UTC
    In addition to BrowserUk's comment: Windows won't let you delete or even rename a file that is open by another process. That's one of the bigger differences between Windows and Unix/Linux.
Re: Checking "Modify" property of the file on windows
by $h4X4_|=73}{ (Monk) on Jul 04, 2016 at 11:33 UTC

    This link can help with some file information stat.