[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [MacPerl] Deleting files??



From: John W. Baxter on Fri, 5 Jan 1996 18:09
+ At 17:06 1/4/96, Hal Wine wrote:
+ >True, but this can be easily handled. Use rmdir() on directories, and
+ >unlock files first, using chmod().
+
+ Easy for a Unix refugee...utter giberish for the rest of us (just memorize
+ the incantations and don't worry about it.
+
Hal pointed out that I hadn't done my homework properly :-). In Unix parlance
rmdir() is "remove directory" and chmod() is "change mode". rmdir() is
obvious, chmod() isn't.

In part (please don't crucify for the deliberate shortening of this explanation
:-), Unix uses a 4-octal number to indicate the status of file. You can see
this when doing a long file listing ("ls -l") which returns some thing like:

    $ ls -l
    -rw-r--r--  1 mark  user   335 12 Jan 95   foo_1
    -rwxr-xr-x  1 mark  user   335 12 Jan 95   foo_2

The "rwx" stuff are flags gleaned from the file's mode: read=4 write=2
execute=1. The 1st grouping is my own permissions, the 2nd my groups,
and the 3rd the rest of the world's. The 4th octal number is special,
check an advanced Unix book for details.

So file foo_1 has chmod() "value" 644 - I can read and write the file, everyone
else has read permission only. To change the permission in perl, I do something
like:

    chmod 760, foo_1

Another listing, and I get

    $ ls -l
    -rwxrw----  1 mark  user   335 12 Jan 95   foo_1
    -rwxr-xr-x  1 mark  user   335 12 Jan 95   foo_2

In Unix-speak, foo_1 is now executable (I can attempt to load and execute it),
write-able by my group and "invisible" to the rest of the world.

So, in terms of the Mac, if I have a locked file, I can do

    chmod 666, file_list

and make it unlocked.

I have included an update of my previous effort. This now removes locked files
and nested folders within a specified folder.

BTW, apologies for the long-winded posting.

---
mark. (mark_probert@optus.com.au ph +61 2 342 8502 fax +61 2 342 7500)


===========================< cut here >============================

#!perl -w
#
# CleanTemp.pl
#
require "FindFolder.pl";                                                        # used to locate directories

$base_dir = &MacPerl'FindFolder("P") . "Perl:";         # base directory pointer
$temp_dir = $base_dir . "Temp";                                         # general temporary store

$debug = 1;
&clean_folder($temp_dir, $temp_dir, 0);


# clean_folder -- recursively clean up a folder
#
sub clean_folder
{
        local($original, $folder, $dispose) = @_;
        print "o=$original f=$folder flag=$dispose\n" if $debug;
        
        chdir $folder;                          # go to the directory
        
        opendir(DIR, $folder);          # get the contents
        local(@list) = readdir(DIR);
        closedir(DIR);
        
        foreach $f (@list)
        {
                print "check: $f" if $debug;
                
                if (-d $f)
                {       
                        print " - recurse\n" if $debug;
                        &clean_folder($folder, "$folder:$f", 1);
                }
                else
                {
                        print " - remove\n" if $debug;
                        chmod 644, $f;                  # easier not to check
                        unlink $f;      
                }
        }
        chdir($original);
        rmdir $folder if $dispose;                                      # delete the directory
}

===========================< cut here >============================