Listing 1. Ownedby.cmd @ECHO OFF :: ----------------------------------------------------------------- :: Filename: ownedby.cmd :: :: Author: Steve Seguis :: :: Purpose: Outputs a list of all files that a user has access to in the specified :: directory and its subdirectories.:: :: Output : Output is dumped to a specified output file. :: :: Syntax : ownedby.cmd :: :: Example: ownedby.cmd JDoe "G:\users" results.txt :: This command outputs to results.txt all files that Jdoe owns :: in G:\users and its subdirectories. If root_directory :: contains spaces in the path, enclose the entire path in :: double quotes. :: :: Requirements: Subinacl.exe and getsid.exe from the Windows 2000 :: or Windows NT Server 4.0 resource kits must be in the same :: directory as the script or in a directory in the PATH environment :: variable. :: ------------------------------------------------------------------ setlocal :: --- Make sure user has specified all three parameters. --- If "%3"=="" Goto syntax :: --- Name of DC --- Set DC=mydc :: --- Store parameters. --- Set USERID=%1 Set ROOTDIR=%2 Set ROOTDIR=%ROOTDIR:"=% Set OUTPUTFILE=%3 If not exist "%ROOTDIR%" ECHO "%ROOTDIR%" Does not exist & Goto syntax If "%DC%"=="mydc" ECHO Please change the DC parameter in the script and rerun & Goto :EOF ' BEGIN CALLOUT A :: --- Get SID for user. --- Set sid= For /f "tokens=7" %%i in ('getsid \\%DC% %USERID% \\%DC% %USERID% ^| find "S-"') Do set sid=%%i ' END CALLOUT A :: --- Ouput header to output file. --- ECHO Listing all files in %rootdir% owned by %userid%... > %OUTPUTFILE% ECHO -------------------------------------------------------------- >> %OUTPUTFILE% ECHO. >> %OUTPUTFILE% :: --- For each file in %ROOTDIR% and all its subdirectories, determine :: whether the file is owned by %USERID%. --- ' BEGIN CALLOUT B For /f "tokens=*" %%i in ('dir /s /b /a-d "%ROOTDIR%"') Do call :checkowner "%%i" endlocal Goto :EOF ' END CALLOUT B :checkowner Set FILENAME=%1 :: --- Use the Subinacl command to output the owner of the file and search :: for the USERID and the user's SID. --- Set owner= For /F "tokens=*" %%j in ('SUBINACL /noverbose /file %FILENAME% ^| find "/owner"') Do set owner=%%j If not defined owner Goto :EOF ' BEGIN CALLOUT C :: --- Output to file if the specified user is found as the owner. --- Set found= ECHO %owner% | find /I "%USERID%" > NUL If %ERRORLEVEL% EQU 0 Set found=1 ' END CALLOUT C ' BEGIN CALLOUT D If not defined sid Goto :nosid ECHO %owner% | find /I "%sid%" > NUL If %ERRORLEVEL% EQU 0 Set found=1 :nosid ' END CALLOUT D ' BEGIN CALLOUT E If defined found ECHO %FILENAME% >> %OUTPUTFILE% ' END CALLOUT E Goto :EOF :syntax ECHO. ECHO Syntax: ownedby.cmd ^ ^ ^ ECHO. ECHO Purpose: Outputs all files in root directory and all its subdirectories ECHO that are owned by userid to file specified. If the root_directory ECHO contains spaces in the path, the entire path should be ECHO enclosed in double quotes. ECHO.