LennyGray
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Another method is to use "Win32_LogicalFileSecurity Setting" and "Win32_LogicalFileOwner" through WMI and VBScript.
File path set in VBS file as "strFile":
cscript //NoLogo "C:\Path_To\GetOwner.vbs" "C:\Path_To\FileName.doc"
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
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
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%
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:
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
@echo off
set var=
for /f "tokens=*" %%a in ('cacls filename.ext') do if not defined var set var=%%a
echo %var%
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
However, you can do it with DIR /Q..
For example:
HTH,
Kent