Jump to content

Who owns the file?


GEOSoft
 Share

Recommended Posts

This is also available on my site under Code>>My Extra UDFs>>Filex.au3 but I thought I would also post it for others to use.

I'm still working on a few related functions to set file permissions etc. I'll post when they are finished.

;
;===============================================================================
; Function Name:   _File_GetOwner()
; Description:   Retrieve the ownership of a file
; Syntax:        _File_GetOwner("FULL path and filename", "computer name")
; Parameter(s):   
; Requirement(s):   
; Return Value(s):   Success - Returns name or group of the file owner
;                           Failure - Returns "" if file not found (@Error = 0)
;                             Sets @Error to 1 if unable to connect to the object
;
; Author(s):   GEOSoft
; Modification(s):   
; Note(s):   
; Example(s):   
;===============================================================================

Func _File_GetOwner($sFile, $sComputer = "localhost")
   Local $Obj, $Item, $Items, $rVal = ""
   If StringRight($sFile, 1) = "\" Then $sFile = StringTrimRight($sFile, 1)
   $sComputer = StringReplace($sComputer,"\","")
   $Obj = ObjGet("winmgmts:\\" & $sComputer & "\root\cimv2")
   If isObj($Obj) Then
   $Items = $Obj.ExecQuery _
   ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & $sFile & "'}" & _
   " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
   For $Item in $Items
      If $Item.ReferencedDomainName Then
         $rVal = $Item.AccountName
      EndIf
   Next
   Else
      SetError(1)
   EndIf
   Return $rVal
EndFunc;<==> _File_GetOwner()
;
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Nice :) I'm sure this will come in handy.

Looking forward to be able to set file (folder?) permissions, could really make life easier.

Thanks.

Now that you mention it, I forgot to check if this function will work against a folder or if I'm going to have to write another one for that.

I already have a method to remove ownership of a file or folder and I think I can use something similar to re-set ownership, but I'm not ready to post that yet because I think there may be an easier way. I'm also working on a file/folder lock/unlock function, that one is giving me trouble.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

The code has been edited so that it will run against a folder properly.

@monoceres

I have the code to set file/folder ownership to the current user working properly. I just have to decide if I should post it here or not. The reason is that I think it could be abused, however I have found similar scripts in other languages so people could use one of those instead I suppose.

I will have to think about that some more.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Shouldn't that be

Failure - Returns "" if file not found (@Error = 0)

8)

Thanks @Val

Typo

I plead the old age defence.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thank you for this usefull code.

Just a remark, if the WMI is not installed (i got the case with some XP embedded) the code crash. This can be handled with:

$oErr = ObjEvent("AutoIt.Error", "onerror")

Cu

Link to comment
Share on other sites

Just dredged up from the depths of my memory ... I think someone who works for MS used to have a free program for download on their own site, that would let you set things, etc. So you are probably ok to post your own version here, without feeling to worried about the devastation it might precipitate! :)

:)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Just dredged up from the depths of my memory ... I think someone who works for MS used to have a free program for download on their own site, that would let you set things, etc. So you are probably ok to post your own version here, without feeling to worried about the devastation it might precipitate! :)

:)

I know that one of the MVPs has something similar on his site. I put the code into the page on my site so you just have to go to the site and follow the menu links...

Code>>My Extra UDFs>>Filex.au3

I'm leaning more to this method of making code available because, according to my counters, people are finding more that they are interested in while they are there.

The site link is in my sig. Of course it's still under construction so some of the menu links (Code Examples, Tips & Tricks) are not working because I have not uploaded the code for any of sub menus yet. In fact I have not even decided what format I want to use for Tips & Tricks yet.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...