4

I have a problem - in the window when the file is opened to show the name of the one who last modified the file. This information is available if you right-click on the file and select Properties and tab Details... i see Owner line and name but i dont know how to get this from my script.

lets see properties on file:

\\server\project\sequences\ttt_sRnd.v016.mb

enter image description here

I use Python2.7 and and I do not find the solution how to get the data... in linux its worked. but not in windows. I tried to console utilities windows.

dir /Q - its worked on local files

C:\temp>dir /Q file.ext
11/06/2004  15:33           290,304 COMP\user       file.ext
               1 File(s)        290,304 bytes
               0 Dir(s)  316,720,226,304 bytes free

but don't worked when file on server:

\\server\project\sequences\>dir /Q file.ext
21/12/2016  16:00            66,372 ...                    file.ext
               1 File(s)         66,372 bytes
               0 Dir(s)  52,561,190,912 bytes free

it's strange, because in the explorer I can see the data and they are available

well, try another utility subinacl.exe

its the same - worked on local files and not worked with file on server:

C:\temp>subinacl.exe /file file.ext /display=owner
/owner             =comp\user

C:\temp>subinacl.exe /file \\server\project\sequences\file.ext  /display=owner
\\server\project\sequences\file.ext - CreateFile Error : 1314 A required privilege is not held by the client.

i try takeown and all the same - work only on local files:

C:\temp>takeown /F file.ext
SUCCESS: The file (or folder): "C:\temp\file.ext" now owned by user "COMP\user".

\\server\project\sequences\>takeown /F file.ext
ERROR: Access is denied.

It may have something else utility in windows? I am ready even to write such a tool myself and call it from python. but I have no idea how to get this information? tell me how to crash problem in any programming language? I believe that in C/С++ or C# code is a matter of the 5-lines with the output to the console ... if so - what will be glad to help, and then I will cause this utility from python

Massimo
  • 836
  • 3
  • 17
  • 38
  • 1
    The [standard lookup](http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html) (which has now been shamelessly plagiarized from Tim's site twice here without attribution) would be `sd = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION);` `sid = sd.GetSecurityDescriptorOwner()`. Then call `win32security.LookupAccountSid(machine, sid)` to map the SID to a domain and name. – Eryk Sun Dec 22 '16 at 11:42
  • From a deleted answer we know the last part fails with `ERROR_TRUSTED_RELATIONSHIP_FAILURE` (1789) because you're passing `None` for the machine name and hitting some configuration problem in the domain controller. Try passing "server" for the machine name to query it directly. – Eryk Sun Dec 22 '16 at 11:42
  • @eryksun the deleted answer is there again, – Ari Gold Dec 22 '16 at 11:44
  • @AriGold, apparently you can't see it, but someone else plagiarized Tim's site and then deleted that answer before you added yours. When you basically copy and paste from someone's site, you need to cite the source, or people will downvote you. – Eryk Sun Dec 22 '16 at 11:46
  • @eryksun its worked! Thank you! ) – Massimo Dec 22 '16 at 14:04
  • @eryksun may by you know how to write it on C++/C#? because install win32 python libraries in Autodesk Maya - its too hard ( i think simple console utility is better way ( – Massimo Dec 23 '16 at 09:20
  • If you have the subprocess module in Maya, then you could run an external Python script using a regular Python installation or WinPython. – Eryk Sun Dec 23 '16 at 09:55
  • @eryksun its need to install python + win32 libs on all computers in studio... its a bad way ( i try sometring like: https://msdn.microsoft.com/en-us/library/windows/desktop/aa446629(v=vs.85).aspx - but it very old code and not worked on win7. it raise error while open file... – Massimo Dec 23 '16 at 10:38

1 Answers1

5

python 2.7

try to use the functions (GetFileSecurity and LookupAccountSid) from the win32security library and you will obtain information about owner

import win32security

def GetOwner(filename):
    f = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION)
    (username, domain, sid_name_use) =  win32security.LookupAccountSid(None, f.GetSecurityDescriptorOwner())
    return username

print GetOwner(r"\\some_shared_location\somefile.txt")
mimin0
  • 871
  • 8
  • 9