JSI Tip 7204. How can I get the DIR command to return a file's owner in a batch script?

Jerold Schulman

September 15, 2003

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

In tip 3253, I discussed using the DIR command's /q switch to display the owner of a file.

I have scripted WhoOwns.bat to parse the owner from the DIR output. The syntax for using WhoOwns.bat is:

call whoowns FileName Owner

where FileName is the fully qualified file name, and Owner is a call directed environment variable that will contain the files owner, using the NetBIOSComputerName_Or_NetBIOSDomainNameUserName format. See the following examples of Owner content:

JSIINCJerry

JSI003Jerry

WhoOwns.bat contains:

@echo offif {%2}
{} @echo Syntax: WhoOwns Filename Owner&goto :EOFif not exist %1 @echo Syntax: WhoOwns Filename Owner - %1 not found&set %2=File_Not_Found&goto :EOFsetlocalfor /f "Tokens=2,3 Delims=[." %%v in ('ver') do set version=%%v.%%wset version=%version:version =%set version=%version:00=0%for /f "Skip=4 Tokens=*" %%f in ('dir /q %1') do set line=%%f&call :parseendlocal&set %2=%owner%&goto :EOF:parseset file=%line:~59%if "%file%"
"" goto :EOFset owner=%line:~36,23%#if "%version%" LSS "5.1" set owner=%line:~39,23%#set owner=%owner:  =%set owner=%owner: #=#%set owner=%owner:#=%



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