Rekurzivní převzetí vlastníka ve Windows 7 pomocí Perlu

neděle, 4. červenec 2010 @ 14:40

Systém oprávnění ve Windows 7 je takový, jaký je. Mně třeba zrovna moc nesedí, hlavně proto, že převzít vlastnictví hromadně v podstatě nejde (ono se to sice tváří že ano ale převezme jen něco). Nakonec jsem tedy sáhl po řešení v podobě skriptu v Perlu.

#!perl
 
# recursive directory listing
use strict;
use File::Find;
 
sub loadFiles();
sub mySub();
 
my @files = ();
my $dir = shift || die "Argument missing: directory name\n";
 
# Replace a string without using RegExp.
sub str_replace
{
    my $search = shift;                         # what to find
    my $replace = shift;                            # what to replace it with
    my $subject = shift;                            # the scalar we are operating on
    if (! defined $subject) { return -1; }      # exit if all three required parameters are missing (!)
    my $count = shift;                          # number of occurrences to replace
    if (! defined $count) { $count = -1; }      # set $count to -1 (infinite) if undefined
 
    # start iterating
    my ($i,$pos) = (0,0);
    while ( (my $idx = index( $subject, $search, $pos )) != -1 )    # find next index of $search, starting from our last position
    {
        substr( $subject, $idx, length($search) ) = $replace;       # replace $search with $replace
 
        $pos=$idx+length($replace);     # jump forward by the length of $replace as it may be
                                        # longer or shorter than $search was, and if we don't
                                        # compensate for this we end up in a different portion
                                        # of the string.
 
        if ($count>0 && ++$i>=$count) { last; }             # stop iterating if we have reached the limit ($count)
    }
 
    return $subject;
}
 
loadFiles();
map { print "$_\n"; } @files;
 
sub loadFiles()
{
    find(\&mySub,"$dir"); #custom subroutine find, parse $dir
}
 
sub mySub()
{
    if(/\.*$/i) {
        my $filename = str_replace('/', '\\', $File::Find::name);
        print $filename."\r\n";
        system("takeown /f \"$filename\"");
        system("icacls \"$filename\" /grant administrators:F");
    }
}

nudists-bashful