JSI Tip 4775. How can I use the command line to determine the ownership of a file or folder?

Jerold Schulman

January 31, 2002

1 Min Read
ITPro Today logo in a gray background | ITPro Today


In tip 3528, we used the command line to set ownership of a file system object.

To determine the ownership of a file system object, I have scripted Owner.bat.

The syntax for using Owner.bat is:

owner

where is the path to the file or folder. Owner.bat returns the owner in an environment variable named $owner.

Examples:

owner "%USERPROFILE%" would set $owner to the owner of the logged on user profile.

owner %Systemroot%system32dllcache would set $owner to builtinadministrators.

owner %logonserver%NETLOGONlogon.bat would set $owner to the owner of the logon.bat file in the NETLOGON share of the authenticating domain controller.

Owner.bat contains:

@echo offif {%1}=={} goto syntaxif not exist %1 goto syntaxsetlocal ENABLEDELAYEDEXPANSIONset $owner=NONEset file=%1set file=%file:"=%set line=Ncall :dod "%file%">nul 2>&1if "%line%" EQU "N" goto dofset $owner=%line:~30%set $owner=%$owner: .=%for /f "Tokens=*" %%o in ('@echo %$owner%') do ( set $owner=%%o)endlocal&set $owner=%$owner%goto :EOF:dodfor /f "Tokens=*" %%a in ('dir %1 /q /ad^|findstr /L /V /c:"&"^|findstr /L /c:""^|shellesc') do ( if "!line!" EQU "N" set line=%%a)goto :EOF:doffor /f "Tokens=*" %%a in ('dir %1 /q /a-d^|findstr /L /V /c:"&"^|findstr /I /L /c:"%~nx1"^|shellesc') do ( set work=%%a)call set work=%%work: %~nx1=%%for /f "Tokens=*" %%a in ('@echo %work:~36%') do ( set $owner=%%a)endlocal&set $owner=%$owner%goto :EOF:syntaxset $owner=NONE@echo Syntax: owner Object



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like