?
Solved

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

Posted on 2011-10-14
6
Medium Priority
?
491 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:LennyGray
6 Comments
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 36972054
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
0
 
LVL 10

Author Comment

by:LennyGray
ID: 36972057
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?
0
 
LVL 11

Accepted Solution

by:
Paolo Santiangeli earned 2000 total points
ID: 36972114
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Expert Comment

by:BillDL
ID: 36972201
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

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36972524
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.
 
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36972535
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

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question