Skip navigation

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


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_NetBIOSDomainName\UserName format. See the following examples of Owner content:

JSIINC\Jerry

JSI003\Jerry

WhoOwns.bat contains:

@echo off
if \{%2\}

\{\} @echo Syntax: WhoOwns Filename Owner&goto :EOF if not exist %1 @echo Syntax: WhoOwns Filename Owner - %1 not found&set %2=File_Not_Found&goto :EOF setlocal for /f "Tokens=2,3 Delims=\[." %%v in ('ver') do set version=%%v.%%w set version=%version:version =% set version=%version:00=0% for /f "Skip=4 Tokens=*" %%f in ('dir /q %1') do set line=%%f&call :parse endlocal&set %2=%owner%&goto :EOF :parse set file=%line:~59% if "%file%"

"" goto :EOF set owner=%line:~36,23%# if "%version%" LSS "5.1" set owner=%line:~39,23%# set owner=%owner: =% set owner=%owner: #=#% set owner=%owner:#=%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish