Link to home
Start Free TrialLog in
Avatar of LennyGray
LennyGrayFlag for United States of America

asked on

How can I directly determine the file owner from the CACLS.exe output?

I need a way for the CACLS.exe output to show only the owner of a file or return one line that can be parsed.
Avatar of Kent Dyer
Kent Dyer
Flag of United States of America image

Don't think you can do what you are thinking of with CACLS..  That is with one line..
C:\>cacls WinDirStat.zip
C:\WinDirStat.zip BUILTIN\Administrators:(ID)F
                  NT AUTHORITY\SYSTEM:(ID)F
                  BUILTIN\Users:(ID)R
                  NT AUTHORITY\Authenticated Users:(ID)C

However, you can do it with DIR /Q..

For example:
C:\>dir /Q WinDirStat.zip
 Volume in drive C has no label.
 Volume Serial Number is 1DE3-AB95

 Directory of C:\

09/11/2010  11:20 AM           561,620 BUILTIN\Administrators WinDirStat.zip
               1 File(s)        561,620 bytes
               0 Dir(s)  324,289,179,648 bytes free

HTH,

Kent
Avatar of LennyGray

ASKER

You are correct about DIR /q but if a user login name is long, the name gets truncated. Do you have any way to stop the truncating of a long name?
ASKER CERTIFIED SOLUTION
Avatar of Paolo Santiangeli
Paolo Santiangeli
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another method is to use "Win32_LogicalFileSecuritySetting" and "Win32_LogicalFileOwner" through WMI and VBScript.

File path set in VBS file as "strFile":
 
On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

strFile = "C:\Documents and Settings\Bill\Desktop\128-128-128.jpg"

Set colItems = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile & "'}" _ 
        & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")

For Each objItem in colItems
    Wscript.Echo objItem.AccountName
Next

Open in new window

Same methods but where you call the VBS file from the command prompt with the file path as the parameter and it sets the argumant as "strFile", eg.
cscript //NoLogo "C:\Path_To\GetOwner.vbs" "C:\Path_To\FileName.doc"
 
On Error Resume Next

set strFile = (WScript.Arguments.Unnamed(0))

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile & "'}" _ 
        & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")

For Each objItem in colItems
    Wscript.Echo objItem.AccountName
Next

Open in new window

You could try something like this:
@echo off

set var=
for /f %%a in ('cacls filename.ext') do if not defined var set var=%%a

echo %var%

Open in new window

NOTE: Replace the 4 occurances of var and 1 occurance of filename.ext with the names of your own variablename and filename.
 
Oops! Missed some detail.... Please try this instead:
@echo off

set var=
for /f "tokens=*" %%a in ('cacls filename.ext') do if not defined var set var=%%a

echo %var%

Open in new window

NOTES:
Replace the 4 occurances of var and 1 occurance of filename.ext with the names of your own variablename and filename.

If there are spaces in the filename then you will need to enclose the filename in double-quotes