Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Cleaning out files for a specified user

Posted by enquirer (enquirer), 19 June 2005
I wonder if I could take up your offer for a bit of assistance?  Basicly I want to implement an equivalent of the unix command "find / -user fred -exec rm {}\;" on a Window 2000 machine.  The package File::Find doesnt seem to have the ability to search for a given user and the best I can envisage is to devise a tree walking algorithm and analise the result of the system command `dir /q/a` to find files owned by a given user (hidden or not). Surely there's a better way in Perl?

If it makes any difference to your solution I wish later to use it over a network on a series of machines whose IP address is held in a table.

Posted by admin (Graham Ellis), 19 June 2005
Here's my "first pass" at a script in Perl that traverses a tree in the way you want.  Purists might point you at a feature of the File::Find module that you mentioned ... personally, I've always found a bit of code like this to be quick and flexible.

Code:
#!/usr/bin/perl

die "Usage: $0 uid dir [dir .... ]\n" unless (@ARGV);

# Add in a call to getpwuid if you want to specify a name
$user = shift (@ARGV);

@queue = @ARGV;

while ($current = shift(@queue)) {
       opendir DH,$current;
       @content = readdir DH;
       foreach $element(@content) {
               next if ($element =~ /^\.\.?$/) ;
               $full = "$current/$element";
               @about = stat($full);
               if (-d _) {
                       push @queue,$full;
                       next;
               }
               if ($about[4] == $user) {
                       push @rmq,$full;
               }
       }
}
# Uncoment following line to do the damage
# unlink @rmq;
print "@rmq\n";


Question - what do you want to do about directories owned by the person? Especially directories that may contain something owned by someone else?   My example removes plain files but leaves the directory structure in place;  you could collect a queue of directores to be removed then write a loop ro send each of them to rmdir in reverse order.

The networking question is a little trickier ... I know what you're trying to do - perhaps it's similar to what I sometimes do after a course at our training centre.  However, network logins and security issues come to mind.  The way we have tackled it is to have a script that we run on each computer we're tidying up in turn - the script really should be "out there" at each system.  You might find a way to remotely trigger it but, boy oh boy, you have a huge scope for deleting a lot of files from a whole network of machines ... get it wrong and - wow - .....

Posted by admin (Graham Ellis), 22 June 2005
It's been reported to me that Perl implementations on Windows systems don't always support the various functions that handle file ownership such as getpwuid with which this program could be extended.

I am NOT a Windows expert (hey - I have enough with knowing all the languages) but I think the solution lies in windows 32 permissions.

use Win32:erms;

$fp = new Win32:erms($pathtofile);
$owner = $fp -> Owner();

See Win-32 Perl Scripting by David Roth which has more on this



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho