Skip navigation

JSI Tip 8870. How can I locate all folders and files that have invalid ownership?


I have scripted OwnerInvalid.bat to report all folders and files that have invalid ownership.

The syntax for using OwnerInvalid.bat is:

OwnerInvalid DriveOrFolder ReportFile

where DriveOrFolder is the starting path, like C: or D: or "C:\Documentents and Settings", and ReportFile is the name of the file that will contain the list of folders and files with invalid ownership.

NOTE: OwnerInvalid.bat uses ShellESC.exe, which must be located in a folder that is in your path.

OwnerInvalid.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: OwnerInvalid DriveOrFolder ReportFile&goto:EOF
if not exist %1 @echo Syntax: OwnerInvalid DriveOrFolder ReportFile - %1 NOT found.&goto:EOF
setlocal ENABLEDELAYEDEXPANSION
set top=%1
set RF=%2
if exist %RF% del /q %RF%
set object=%top%
set object=%object:"=%
for /f "Tokens=*" %%a in ('dir %top% /ad /q^|findstr /L /V /c:"&"^|findstr /L /c:" ... "^|shellesc') do (
 set line=%%a
 for /f "Tokens=*" %%x in ('@echo !line:~46!#^|findstr /L /c:" .#"^|shellesc') do (
  @echo "%object%" >>%RF%
 )
)
for /f "Tokens=*" %%f in ('dir %top% /s /b /ad') do (
 call :treeD "%%f" >nul 2>&1
)
for /f "Tokens=*" %%f in ('dir %top% /s /b /a-d') do (
 call :treeF "%%f" >nul 2>&1
)
endlocal
goto :EOF
:treeD
for /f "Tokens=*" %%a in ('dir %1 /q /ad^|findstr /L /V /c:"&"^|findstr /L /c:" ... "^|shellesc') do (
 set line=%%a
 for /f "Tokens=*" %%x in ('@echo !line:~46!#^|findstr /L /c:" .#"^|shellesc') do (
  @echo %1 >>%RF%
 )
)
goto :EOF
:treeF
for /f "Tokens=*" %%a in ('dir %1 /q /a-d^|findstr /L /V /c:"&"^|findstr /L /c:" ... "^|findstr /I /L /c:"%~nx1"^|shellesc') do (
 @echo %1 >>%RF%
)



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