get file owner in batchfile

A

AMD

hi,

I got this batchfile in your previous discussion. Can
anyone show me how to include the owner of the file in
this batchfile. I know I can use "dir /q" to view the
owner of the file but I do not know how to add in the
owner of the file in this batchfile.


@echo off
::
:: shows the created, accessed, and written dates for
:: the specified file(s) (parameter %1)
::
if {%1} NEQ {} if {%1} NEQ {?} if {%1} NEQ {/?} if /I {%1}
NEQ {HELP} goto :begin

call XHELP %~f0
goto :EOF

:begin
if not exist %1 echo File %1 not found....&goto :EOF
setlocal
set filespec=%1
set filepath=%~dp1
echo.
echo Created Last Accessed Last Written Filename
echo.
for /f "tokens=*" %%a in ('dir /b /a-d %filespec%') do
call :dates "%%a" endlocal&goto :EOF
:dates
set thisfile=%1
set thisfile=%thisfile:&=^&%
set nameonly=%thisfile:"=%
set file="%filepath%%nameonly%"
for %%f in (c a w) do (for /f "tokens=1,2" %%a in ('dir /t%
%f %file%^| find "/"') do set %%f_date=%%a %%b)
echo %c_date% %a_date% %w_date% %file%
goto :EOF

Needed output:

echo.
echo Created Last Accessed Last Written Filename Owner
echo.


thank you very much,
AMD
 
P

Phil Robyn

AMD said:
hi,

I got this batchfile in your previous discussion. Can
anyone show me how to include the owner of the file in
this batchfile. I know I can use "dir /q" to view the
owner of the file but I do not know how to add in the
owner of the file in this batchfile.


@echo off
::
:: shows the created, accessed, and written dates for
:: the specified file(s) (parameter %1)
::
if {%1} NEQ {} if {%1} NEQ {?} if {%1} NEQ {/?} if /I {%1}
NEQ {HELP} goto :begin

call XHELP %~f0
goto :EOF

:begin
if not exist %1 echo File %1 not found....&goto :EOF
setlocal
set filespec=%1
set filepath=%~dp1
echo.
echo Created Last Accessed Last Written Filename
echo.
for /f "tokens=*" %%a in ('dir /b /a-d %filespec%') do
call :dates "%%a" endlocal&goto :EOF
:dates
set thisfile=%1
set thisfile=%thisfile:&=^&%
set nameonly=%thisfile:"=%
set file="%filepath%%nameonly%"
for %%f in (c a w) do (for /f "tokens=1,2" %%a in ('dir /t%
%f %file%^| find "/"') do set %%f_date=%%a %%b)
echo %c_date% %a_date% %w_date% %file%
goto :EOF

Needed output:

echo.
echo Created Last Accessed Last Written Filename Owner
echo.


thank you very much,
AMD

You have too many errors in your 'copy' of the original; the code
that you posted will not work unless the errors are fixed. Here's
the original:

=====begin c:\cmd\UTIL\DIRDATES.CMD ====================
01. @echo off
02. ::
03. :: shows the created, accessed, and written dates for
04. :: the specified file(s) (parameter %%1)
05. ::
06. if {%1} NEQ {} if {%1} NEQ {?} if {%1} NEQ {/?}^
07. if /i {%1} NEQ {HELP} goto :begin
08. call XHELP %~f0
09. goto :EOF
10.
11. :begin
12. if not exist %1 echo File %1 not found....&goto :EOF
13. setlocal
14. set filespec=%1
15. set filepath=%~dp1
16. echo.
17. echo Created Last Accessed Last Written Filename
18. echo --------------- --------------- --------------- ----------------------------------
19. for /f "tokens=*" %%a in (
20. 'dir /b /a-d %filespec%'
21. ) do call :dates "%%a"
22. endlocal&goto :EOF
23. :dates
24. set thisfile=%1
25. set thisfile=%thisfile:&=^&%
26. set nameonly=%thisfile:"=%
27. set file="%filepath%%nameonly%"
28. for %%f in (c a w) do (
29. for /f "tokens=1,2" %%a in (
30. 'dir /t%%f %file%
31. ^| find "/"'
32. ) do set %%f_date=%%a %%b
33. )
34. echo %c_date% %a_date% %w_date% %file%
35. goto :EOF
=====end c:\cmd\UTIL\DIRDATES.CMD ====================

Here's output from a modified version showing the owner:

<Win2000> c:\cmd>demo\DirDates demo\date*

Created Last Accessed Last Written Owner Filename
--------------- --------------- --------------- -------- ----------------------------------
06/19/02 11:54a 11/02/03 12:00a 06/19/02 11:54a Everyone "c:\CMD\DEMO\DateAsFileName.cmd"
09/22/01 12:48p 11/02/03 12:00a 09/22/01 12:48p Everyone "c:\CMD\DEMO\dateinfo.001"
09/22/01 06:19p 11/02/03 12:00a 09/22/01 05:59p Everyone "c:\CMD\DEMO\dateinfo.002"
09/22/01 10:34p 11/02/03 12:00a 09/22/01 06:43p Everyone "c:\CMD\DEMO\dateinfo.003"
10/02/01 11:54p 11/02/03 12:00a 10/02/01 11:53p Everyone "c:\CMD\DEMO\dateinfo.004"
09/22/01 12:48p 12/09/03 12:00a 10/02/01 11:55p Everyone "c:\CMD\DEMO\dateinfo.cmd"
10/29/02 09:57a 11/02/03 12:00a 11/16/02 03:25p Everyone "c:\CMD\DEMO\dateyyyymmdd.cmd"
11/20/01 09:23a 11/02/03 12:00a 11/20/01 09:23a Everyone "c:\CMD\DEMO\datentime.cmd"
01/24/02 09:17p 11/02/03 12:00a 01/24/02 05:16p Everyone "c:\CMD\DEMO\datemath.001"

<Win2000> c:\cmd>rlist demo\DirDates.cmd
=====begin c:\cmd\demo\DirDates.cmd ====================
01. @echo off
02. ::
03. :: shows the created, accessed, and written dates for
04. :: the specified file(s) (parameter %%1)
05. ::
06. if {%1} NEQ {} if {%1} NEQ {?} if {%1} NEQ {/?}^
07. if /i {%1} NEQ {HELP} goto :begin
08. call XHELP %~f0
09. goto :EOF
10.
11. :begin
12. if not exist %1 echo File %1 not found....&goto :EOF
13. setlocal
14. set filespec=%1
15. set filepath=%~dp1
16. echo.
17. echo Created Last Accessed Last Written Owner Filename
18. echo --------------- --------------- --------------- -------- ----------------------------------
19. for /f "tokens=*" %%a in (
20. 'dir /b /a-d %filespec%'
21. ) do call :dates "%%a"
22. endlocal&goto :EOF
23. :dates
24. set thisfile=%1
25. set thisfile=%thisfile:&=^&%
26. set nameonly=%thisfile:"=%
27. set file="%filepath%%nameonly%"
28. for %%f in (c a w) do (
29. for /f "tokens=1,2" %%a in (
30. 'dir /t%%f %file%
31. ^| find "/"'
32. ) do set %%f_date=%%a %%b
33. )
34. for /f "tokens=4" %%a in (
35. 'dir /q %file% ^| find "/"'
36. ) do set owner=%%a
37. echo %c_date% %a_date% %w_date% %owner% %file%
38. goto :EOF
=====end c:\cmd\demo\DirDates.cmd ====================

And here's the XHELP batch file:

=====begin c:\CMD\TEST\xhelp.cmd ====================
01. @echo off
02. ::
03. :: Displays the contents of the 'flowerbox' in a CMD or BAT
04. :: file.
05. ::
06. :: '%~n0' will be displayed as the name of the referenced file
07. ::
08. echo .?./?.-?.help.. | findstr /i ".%1." > nul
09. if %errorlevel% EQU 0 (
10. call XHELP %~f0
11. goto :EOF
12. )
13. setlocal
14. set cd=
15. set whichfile=%1
16. for /f "tokens=*" %%a in (
17. 'dir %whichfile% ^| find "/"'
18. ) do set direntry=%%a
19. set fname=%~n1
20. echo :
21. echo : %direntry:~0,38% %whichfile%
22. echo :
23. for /f "tokens=* delims=" %%* in (
24. 'findstr /b /c:"::" %whichfile%') do call :display %%*
25. endlocal& goto :EOF
26. :display
27. set line=%*
28. set line=%line:~1%
29. if not defined cd set line=%line:~1%
30. call set line=%%line:^%~n0=%fname%%%
31. echo %line%
32. goto :EOF
=====end c:\CMD\TEST\xhelp.cmd ====================
 
G

Guest

sorry about that Phil. thank you very much
-----Original Message-----


You have too many errors in your 'copy' of the original; the code
that you posted will not work unless the errors are fixed. Here's
the original:

=====begin c:\cmd\UTIL\DIRDATES.CMD ====================
01. @echo off
02. ::
03. :: shows the created, accessed, and written dates for
04. :: the specified file(s) (parameter %%1)
05. ::
06. if {%1} NEQ {} if {%1} NEQ {?} if {%1} NEQ {/?}^
07. if /i {%1} NEQ {HELP} goto :begin
08. call XHELP %~f0
09. goto :EOF
10.
11. :begin
12. if not exist %1 echo File %1 not found....&goto :EOF
13. setlocal
14. set filespec=%1
15. set filepath=%~dp1
16. echo.
17. echo Created Last Accessed Last Written Filename
18. echo --------------- --------------- --------------- - ---------------------------------
19. for /f "tokens=*" %%a in (
20. 'dir /b /a-d %filespec%'
21. ) do call :dates "%%a"
22. endlocal&goto :EOF
23. :dates
24. set thisfile=%1
25. set thisfile=%thisfile:&=^&%
26. set nameonly=%thisfile:"=%
27. set file="%filepath%%nameonly%"
28. for %%f in (c a w) do (
29. for /f "tokens=1,2" %%a in (
30. 'dir /t%%f %file%
31. ^| find "/"'
32. ) do set %%f_date=%%a %%b
33. )
34. echo %c_date% %a_date% %w_date% %file%
35. goto :EOF
=====end c:\cmd\UTIL\DIRDATES.CMD ====================

Here's output from a modified version showing the owner:

<Win2000> c:\cmd>demo\DirDates demo\date*

Created Last Accessed Last Written Owner Filename
--------------- --------------- --------------- -------- - ---------------------------------
06/19/02 11:54a 11/02/03 12:00a 06/19/02 11:54a
Everyone "c:\CMD\DEMO\DateAsFileName.cmd"
09/22/01 12:48p 11/02/03 12:00a 09/22/01 12:48p
Everyone "c:\CMD\DEMO\dateinfo.001"
09/22/01 06:19p 11/02/03 12:00a 09/22/01 05:59p
Everyone "c:\CMD\DEMO\dateinfo.002"
09/22/01 10:34p 11/02/03 12:00a 09/22/01 06:43p
Everyone "c:\CMD\DEMO\dateinfo.003"
10/02/01 11:54p 11/02/03 12:00a 10/02/01 11:53p
Everyone "c:\CMD\DEMO\dateinfo.004"
09/22/01 12:48p 12/09/03 12:00a 10/02/01 11:55p
Everyone "c:\CMD\DEMO\dateinfo.cmd"
10/29/02 09:57a 11/02/03 12:00a 11/16/02 03:25p
Everyone "c:\CMD\DEMO\dateyyyymmdd.cmd"
11/20/01 09:23a 11/02/03 12:00a 11/20/01 09:23a
Everyone "c:\CMD\DEMO\datentime.cmd"
01/24/02 09:17p 11/02/03 12:00a 01/24/02 05:16p
Everyone "c:\CMD\DEMO\datemath.001"
<Win2000> c:\cmd>rlist demo\DirDates.cmd
=====begin c:\cmd\demo\DirDates.cmd ====================
01. @echo off
02. ::
03. :: shows the created, accessed, and written dates for
04. :: the specified file(s) (parameter %%1)
05. ::
06. if {%1} NEQ {} if {%1} NEQ {?} if {%1} NEQ {/?}^
07. if /i {%1} NEQ {HELP} goto :begin
08. call XHELP %~f0
09. goto :EOF
10.
11. :begin
12. if not exist %1 echo File %1 not found....&goto :EOF
13. setlocal
14. set filespec=%1
15. set filepath=%~dp1
16. echo.
17. echo Created Last Accessed Last Written Owner Filename
18. echo --------------- --------------- --------------- -
------- ----------------------------------
 
A

AMD

how about if I got a lot of files listed in a textfile.
How do I get file owner of each file.

example:
Contents of test1.txt are

file1
file2
file3

thanks
 
J

Jerold Schulman

how about if I got a lot of files listed in a textfile.
How do I get file owner of each file.

example:
Contents of test1.txt are

file1
file2
file3

thanks

@echo off
setlocal
for /f "Tokens=*" %%a in (c:\zipnew\test1.txt) do (
set file=%%a
call :getowner
)
endlocal
goto :EOF
:getowner
set owner=
for /f "Skip=4 Tokens=3*" %%b in ('dir /q /a-d "%file%"') do (
if not defined owner set owner=%%c
)
if "%owner%"=="" @echo %file has no owner&goto :EOF
call set owner="%%owner: %file%=%%"
set owner=%owner: =%
set owner=%owner: "="%
@echo "%file%" is owned by %owner%

Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top