Hi

You wrote:
Hi!  I'm a new user of PERL and I'm trying to write a script to give me a
condensed list of file permissions of an NTFS file system.  For example, if
I have a folder named E:\, with a hundred subfolders (and so on), I'd like
to get a list of subfolder names with their associated permissions (ACLs).
If possible, I'd like to leav out duplicates.  For example, if E:\temp has
subfolders going down five levels, but each of the subfolders of E:\temp has
the same permissions as E:\temp, then I want the script to just give me the
permisions on the E:\temp folder.  I tried using the find2perl -exec cacls
{} \; command to generate a script for me to modify, but I'm getting
problems with it (and I'm running out of time).

I've search CPAN's Scripts section but couldn't find anything.  If someone
can point me to alternative archives where I could search I'd appreciate it.
Or if you have a script that does this, even better.  Thanks a lot in
advance!


You may be able to modify this script which goes down all the sub-folders 
looking for stuff: Good luck :-)

--------------
#!/usr/bin/perl -w
use strict;

my $help = <<'__';
########################################################################
#    --------------------------------------------------------
#    Path Directory
#    --------------------------------------------------------
#    Utility to list all the files and their size in a folder
#                     and its sub folders.
#    --------------------------------------------------------
#
#
#    To install:
#    ----------
#    Place the files PathDirectory.pl and pdir.bat in a folder
#    that is included in the machine's PATH. You'll also need perl from
#    http://www.activestate.com/Products/ActivePerl/Download.html
#
#    To use:
#    ------
#    To run from command prompt:
#
#        pdir <folder> <minsize> <clustersize> <age> <savetofile>
#
#    where:
#
#        <folder>       is the folder name (eg, c:\)
#        <minsize>      restricts reported files to those of this size or more
#        <clustersize>  calculates the actual amount of disc space used
#        <age>          any value (eg, 1) will include age of file in days
#        <savetofile>   saves the summary to file with file name <savetofile>
#
#    Note: Only folders with files are declared.
#
#    For example:
#    -----------
#
#        pdir "c:\documents and settings"
#        pdir c: 1000000
#        pdir c: 0 0 1 c:\log.txt
#        pdir c: 0 512 0 c:\log.txt
#
#    To print this message enter ? as the first parameter
#
#    Author: Ian Summers [EMAIL PROTECTED]
#    Date:   27 January 2001
#
#    --------------------------------------------------------
########################################################################
__

my $folder = $ARGV[0];

if ($folder =~ /\?/)
{
         $help =~ s/#//g;
         print $help;
         exit;
}

while (!$folder)
{
         print 'Folder: ';
         $folder = <STDIN>;
         chomp $folder;
}

if (!($folder =~ /\\$/))
{
         $folder .= '\\';
}

my $minsize = $ARGV[1];
my $clustersize = $ARGV[2];
my $datemod = $ARGV[3];
my $savedata = $ARGV[4];

my $append = '>';
my $error = '';

if (!$minsize)
{
         $minsize = 0;
}

printit ("\nSummary of the files in the chosen path (including its 
sub-folders)");
if ($minsize)
{
         printit ("\nif their size is >= ".filesize($minsize,1).' bytes');
}
printit ("\nOn ".filedate(time)."\n");
if ($clustersize)
{
         printit ("\n'Used in Cluster' based on a cluster size of 
$clustersize bytes.\n");
}
if ($datemod)
{
         printit ("\nThe age of each file as the number of days since 
modified is given.\n");
}
printit ("\n\nFile size bytes");
if ($clustersize)
{
         printit ("  Used in Cluster");
}
if ($datemod)
{
         printit ('    Age');
}
printit ("  File Name\n---------------");
if ($clustersize)
{
         printit ("  ---------------");
}
if ($datemod)
{
         printit ('  -----');
}
printit ("  ----------------------\n");

my $totalfilesize = 0;
my $totalclusterfilesize = 0;
my $grandtotalfilesize = 0;
my $grandtotalclusterfilesize = 0;
my $delcarefolder;
my $somefiles = 0;

&look ($folder);


printtotal ($totalfilesize, $totalclusterfilesize, '');
printtotal ($grandtotalfilesize, $grandtotalclusterfilesize, "\n\nGrand 
total:\n\n");


# -------------------------------------------------------------

sub look
{
         my $currentfolder = $_[0];
         if ($somefiles)
         {
                 $delcarefolder .= "\n\n$currentfolder\n\n";
         } else {
         }
                 $delcarefolder = "\n\n$currentfolder\n\n";
         my $first = 1;
         if (opendir DIR, $currentfolder)
         {
                 my @folderfiles = grep !/^\.\.?$/, readdir DIR;
                 closedir DIR;

                 # loop through files
                 foreach (sort {lc($a) cmp lc($b)} @folderfiles)
                 {
                         chomp;
                         my $filename = $_;
                         my $folderfilename = "$currentfolder\\$filename";
                         if (!(-d $folderfilename))
                         {
                                 my $filesize = -s $folderfilename;
                                 if ($filesize >= $minsize)
                                 {
                                         if ($first)
                                         {
                                                 if ($totalfilesize)
                                                 {
                                                         printtotal 
($totalfilesize, $totalclusterfilesize, '');
                                                         printit ($error);
                                                         $totalfilesize = 0;
                                                         $totalclusterfilesize 
= 0;
                                                 }
                                                 printit ($delcarefolder);
                                                 $delcarefolder = '';
                                                 $first = 0;
                                                 $somefiles = 0;
                                         }
                                         $totalfilesize += $filesize;
                                         printit (filesize($filesize));
                                         if ($clustersize)
                                         {
                                                 my $fileclustersize = 
int($filesize/$clustersize)*$clustersize;
                                                 if ($fileclustersize != 
$filesize)
                                                 {
                                                         $fileclustersize 
+= $clustersize;
                                                 }
                                                 $totalclusterfilesize += 
$fileclustersize;
                                                 printit 
(filesize($fileclustersize));
                                         }
                                         if ($datemod)
                                         {
                                                 my $filedate = -M 
$folderfilename;
                                                 printit (filedays($filedate));
                                         }
                                         printit ("$filename\n");
                                         $somefiles = 1;
                                 }
                         }
                 }

                 # loop through folders
                 foreach (sort {lc($a) cmp lc($b)} @folderfiles)
                 {
                         chomp;
                         my $filename = $_;
                         my $folderfilename = "$currentfolder\\$filename";
                         $folderfilename =~ s#\\\\#\\#;
                         if (-d $folderfilename)
                         {
                                 &look ($folderfilename);
                         }
                 }
         } else {
                 $error = "\n\n**** Can't open folder $currentfolder - $!\n\n";
         }
}

sub filesize
{
         $_ = $_[0];
         my $left = $_[1];
         while (s/^(.*?\d)(\d\d\d)((,\d\d\d)*)$/$1,$2$3/) {}
         if ($left)
         {
                 $_;
         } else {
                 sprintf ('%15s  ',$_);
         }
}

sub filedate
{
         $_ = $_[0];
         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
localtime($_);
         $year += 1900;
         my @months = qw"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec";
         my $month = $months[$mon];
         my @thetime = ($hour,$min);
         my $sessiontime = sprintf ('%2.2d:%2.2d',@thetime);
         my @thedate = ($mday,$month,$year);
         my $sessiondate = sprintf ('%2.2d %3s %4.4d',@thedate);
         "$sessiondate $sessiontime  ";
}

sub filedays
{
         sprintf ('%5d',$_[0]).'  ';
}

sub printit
{
         my $message = $_[0];
         if ($savedata)
         {
                 open OUT, "$append$savedata" or die "**** Can't open 
$savedata - $!\n\n";
                 $append = '>>';
                 print OUT $message;
                 close OUT;
         }
         print $message;
}

sub printtotal
{
         my $tfs = $_[0];
         my $tcfs = $_[1];
         my $heading = $_[2];
         printit ("$heading---------------  ");
         if ($clustersize)
         {
                 printit ("---------------");
         }
         printit ("\n".filesize($tfs));
         if ($clustersize)
         {
                 printit (filesize($tcfs));
         }
         printit ("\n");
         $grandtotalfilesize += $totalfilesize;
         $grandtotalclusterfilesize += $totalclusterfilesize;
}




-------- End of script

You'll probably need to use stat (see Programming Perl p224) instead of -s 
$folderfilename at line 160. All my script was interested in was the modify 
date and size which I could do without stat. Still the script does the 
iterative looping of folders - you'll have to do your own fancy stuff that 
you need.

It's not the worlds best perl script but it works.


Have fun

Ian


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom
they are addressed. If you have received this email in error
please notify ISData Ltd:
         mailto:[EMAIL PROTECTED]
         Tel: +44(0)1603 473282
         Fax: +44(0)1603 462181
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Reply via email to