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:



WhoOwns.bat contains:

@echo off
if \{%2\}<h1><a name="_echo_Syntax_WhoOwns_Filename_Owner_amp_goto_EOF_if_not_exist_1_echo_Syntax_WhoOwns_Filename_Owner_1_not_found_amp_set_2_File_Not_Found_amp_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_amp_call_parse_endlocal_amp_set_2_owner_amp_goto_EOF_parse_set_file_line_59_if_file_">\{\} @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
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
set file=%line:~59%
if "%file%"</a></h1>"" 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:#=%