JSI Tip 9085. What files and sub-folders are owned by a specified user?


Using only standard commands, I have scripted Owns.bat to enumerate the files and sub-folders that are owned by a specified user.

The syntax for using Owns.bat is:

Owns Who Results DriveOrFolder1 \[DriveOrFolder2 ... DriveOrFolderN\]

where:

Who           is the specified users, using DomainName\UserId or ComputerName\UserId format, or any portion thereof.

Results       is the .CSV file that will contain the full path of each file system object that is owned by Who, preceded by:
              "D" - Directory
              "F" - File  

DriveOrFolder is the drive letter or folder path to search. You may specify as many search paths as you wish.
Owns.bat contains:
@echo off
setlocal ENABLEDELAYEDEXPANSION
if \{%3\}

\{\} goto syntax if not exist %3 goto syntax set who=%1 set who=%who:"=% set results=%2 if exist %results% del /q %results% set vb="%TEMP%\Owns_%RANDOM%.VBS" @echo dim oString, objArgument>%vb% @echo Set objArgument = Wscript.Arguments>>%vb% @echo P2 = objArgument(1)>>%vb% @echo P3 = objArgument(2)>>%vb% @echo oString = Replace(objArgument(0), P2, P3)>>%vb% @echo Wscript.echo "*:" ^& oString>>%vb% :loop if \{%3\}

\{\} goto finish set folder=%3 set folder=%folder:"=% if not exist %folder% goto syntax shift call :dod "%folder%" for /f "Tokens=*" %%f in ('dir %folder% /b /s /a^|findstr /V /L "& )"') do ( call :dod "%%f" ) goto loop :finish if exist %vb% del /q %vb% endlocal goto :EOF :dod set line=N for /f "Tokens=*" %%a in ('dir %1 /q /a^|FIND "/"') do ( if "!line!" EQU "N" set line=%%a ) set work=%~ftza1 set type=F if "%work:~0,1%" NEQ "d" goto dod1 set type=D for /f "Tokens=1* Delims=:" %%a in ('cscript //NOLOGO %vb% "%line%" "<DIR>" "#DIR#"') do ( set line=%%b ) :dod1 set OK=N for /f "Tokens=*" %%a in ('@echo %line%^|Findstr /I /L /C:"%who%"') do ( set OK=Y ) If "%OK%" EQU "N" goto :EOF @echo "%type%",%1>>%results% goto :EOF :syntax @echo Syntax: owns Who Results Folder endlocal



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