Listing 1: Findownfiles.cmd @Echo Off :: BEGIN CALLOUT A If #%3#==## Goto :syntax Set targetdir=%~1 Set userid=%~2 Set outputfile=%3 If Not Exist %targetdir% Goto :syntax :: END CALLOUT A Echo Files owned by %userid% in %targetdir% and its subdirectories > %outputfile% Echo ---------------------------------------------------------------- >> %outputfile% :start :: BEGIN CALLOUT B For /f "tokens=*" %%i in ('dir /s /b /a-d "%targetdir%"') Do Call :checkfile "%%i" :: END CALLOUT B Goto :EOF :checkfile Set targetfilepath=%~1 Set targetfilename=%~n1 Echo Checking %targetfilepath%... :: BEGIN CALLOUT C Set owner= For /f "tokens=2 delims=\" %%a in ('dir /q "%targetfilepath%" ^| Find /I "%targetfilename%" ^| Find "/" ^| Find "\"') Do Set owner=%%a For /f "tokens=1" %%a in ('Echo "%owner%"') Do Set owner=%%a set owner=%owner:"=% If Not Defined owner goto :EOF :: END CALLOUT C :: BEGIN CALLOUT D If /I "%owner%"=="%userid%" Echo %targetfilepath% >> %outputfile% :: END CALLOUT D Goto :EOF :syntax Echo. Echo Syntax: findownedfiles.cmd ^ ^ ^ Echo. Echo Example: findownedfiles.cmd E:\Users testuser c:\results.txt Echo. Goto :EOF