List Files with Ownership

From IronPython Cookbook

The following code demonstrates how to write a small utility in IronPython that lists files and directories to the standard output, each along with its owner.

import sys
from System.IO import DirectoryInfo, Path, FileAttributes, IOException
from System import Environment, UnauthorizedAccessException, InvalidOperationException
from System.Environment import NewLine
from System.Security.AccessControl import AccessControlSections
from System.Security.Principal import NTAccount

def getowner(fsentry, default = None):
        fss = fsentry.GetAccessControl(AccessControlSections.Owner)
        return fss.GetOwner(NTAccount).Value
    except (IOException, UnauthorizedAccessException, InvalidOperationException):
        return default

def main(args):
    path = not args and Environment.CurrentDirectory or args[0]
    XFLAGS = FileAttributes.Hidden | FileAttributes.System
    fsentries = [fsentry for fsentry in DirectoryInfo(path).GetFileSystemInfos() if not (fsentry.Attributes & XFLAGS)]
    for fsentry in fsentries:
            isdir = type(fsentry) is DirectoryInfo
            name = isdir and fsentry.Name + str(Path.DirectorySeparatorChar) or fsentry.Name
            print '%30s - %s' % (getowner(fsentry, '???'), name)
        except IOException, UnauthorizedAccessException:

if __name__ == '__main__':

Recommended Usage (Windows)

The recommended way to try the above code is to save it in a file named (so called because it sort of emulates the Windows Command Prompt command DIR with the /Q switch). In the same directory as, create a batch file called dirq.bat and place the following text in there:

@ipy "" %*

This assumes that the console version of the IronPython executable (ipy.exe) is in your PATH environment variable. Next, just type dirq on the Command Prompt. To see the files, directories and owners of another directory other than the current, specify the path to the directory as the first argument (e.g. dirq C:\WINDOWS).

See also: Deployment#Batch Files

Sample Output

Below is an example of what the output from the IronPython script could look like. Notice that three question marks (???) where the owner could not be determined.

BUILTIN\Administrators - addins\
BUILTIN\Administrators - AppPatch\
BUILTIN\Administrators - Cluster\
BUILTIN\Administrators - Config\
BUILTIN\Administrators - Connection Wizard\
BUILTIN\Administrators - Cursors\
BUILTIN\Administrators - Downloaded Installations\
BUILTIN\Administrators - Driver Cache\
BUILTIN\Administrators - DTS9_KB933508_ENU\
BUILTIN\Administrators - DTS9_KB934458_ENU\
BUILTIN\Administrators - ehome\
BUILTIN\Administrators - Help\
BUILTIN\Administrators - Hotfix\
                   ??? - ie7updates\
BUILTIN\Administrators - IIS Temporary Compressed Files\
BUILTIN\Administrators - Symbols\
BUILTIN\Administrators - system\
BUILTIN\Administrators - system32\
                   ??? - Temp\
BUILTIN\Administrators - twain_32\
BUILTIN\Administrators - WBEM\
BUILTIN\Administrators - Web\
BUILTIN\Administrators - WinSxS\
BUILTIN\Administrators - win.ini
BUILTIN\Administrators - WindowsUpdate.log
BUILTIN\Administrators - winhelp.exe
BUILTIN\Administrators - winhlp32.exe
BUILTIN\Administrators - winscp.RND
BUILTIN\Administrators - WMFDist11.log
BUILTIN\Administrators - wmp11.log
BUILTIN\Administrators - wmsetup.log
BUILTIN\Administrators - wmsetup10.log
BUILTIN\Administrators - WMSysPr9.prx
BUILTIN\Administrators - wplog.txt
BUILTIN\Administrators - Wudf01000Inst.log
BUILTIN\Administrators - XpsEPSC.log
BUILTIN\Administrators - xpsp1hfm.log
BUILTIN\Administrators - Zapotec.bmp

Back to Contents.