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

Re: [MacPerl] more on File globbing



Hi!

I think I've got an answer for this one, because I've got a bit of
experience in building and removing directory structures (even though my
method doesn't use globs).

>>1.  Check for errors: unlink($_) || die #!;
>>2.  Check filename:   print $_, "\n";
>>

Remember to feed FULL PATHNAMES to any file I/O operation... Even if you
can get it to work without them, you wouldn't want to "fall" out of your
"working directory" when iterating through unlink()!!!

Next, the most important thing of all is that you need to have your lists
of files and folders in the right order:

Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER1
Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER1:file1.txt
Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER1:file2.doc
Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER1:FOLDER1-1
Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER1:FOLDER1-1:file1
Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER1:FOLDER1-1:file2
Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER2
Dumb Computer Name:Desktop Folder:TRASH CAN:FOLDER2:file1

The trick (in case it isn't obvious) is to build the list folders first,
and then the files. Read this list forwards and you can BUILD the entire
tree, read it backwards, and you can NUKE the whole tree, using unlink()...
 Think about  it... You've got to delete all of the files before you can
delete the folder, right? And when you're building, you HAVE to create the
folder before you can put files in it.

Here's a little bit that I wrote called "Faster Trasher" (just for kicks--
I don't think it's much faster, if at all):

(Hmmm, just noticed that it's quite a bit convoluted.... One of the first
scripts that I ever wrote! Also, I don't have a "company Mac" here to
double-check my code, so please forgive any possible omissions! (My PC
wouldn't make for a valid enough test bed!) )

# You'll probably HAVE to remove the line breaks for this to work: #
# Ask the user for the Source folder. #
 $begin = MacPerl::Pick("What do you want to do today?","NEW - Read and
copy simultaneously!","Copy all new files \(Pretty Slow\)","Delete
Stragglers \(Faster\)","Quit \(Cancel works, too\!\)","Faster Trasher","Get
Bookmarks Folder");

&faster_trasher if $begin eq "Faster Trasher";

sub faster_trasher
{
	$hostname = `pwd`; @path = split(/:/, $hostname); $hostname = $path[0];
	###############################################################	
	$tmp_trash_folder = "$hostname:Trash";
	chdir($tmp_trash_folder);
	&do_it($tmp_trash_folder);
	$elapsed = ($finish - $start);
	
	 MacPerl::Answer("That took $elapsed seconds to execute\.\nStart time was
$start\, 
	 and\n Finish time was $finish", "OK");
	
	$again = MacPerl::Answer("Go Again?", "Yes", "No") unless ($begin == 0);
	exec("./faster_trasher.pl") if ($again == 1);
	
	sub do_it {
		local($killing_ground) = @_;
		$begin = MacPerl::Answer("What do you want to do?","Empty Trash","Quit");
		if ($begin == 1) { 
			$killing_ground = MacPerl::Ask("WORKING FOLDER\:","$killing_ground");
			local(@FILES) = &get_files($killing_ground);
			@FILES = reverse sort @FILES;
			$answer = MacPerl::Pick("DELETE?", "Go for it!", "Print Out Files,
First...","Quit");
			if ($answer eq "Go for it!") {
				local($action) = MacPerl::Answer("REALLY?", "OK", "Cancel");
				$start  = time;
				&unlink_files(@FILES) if $action;
				$finish = time; 
			} elsif ($answer =~ /print/i) {
				print join("\n", @FILES); print "\n";
				local($action) = MacPerl::Answer("Delete these?", "OK", "Cancel");
				$start  = time;
				&unlink_files(@FILES) if $action;
				$finish = time; 
			} else { $begin = 0; }
		} 
		elsif ($begin == 0) { return; }
	}
	
	sub unlink_files {
		$i = $#_;
		print "Total no. files: $i\n";
		foreach (@_) { 
			unless ($_ eq $killing_ground) {
				unlink && print "Deleting \# $i $_\n" if !-d;
				rmdir  && print "Removing \# $i $_\n" if -d;
		#		pop;
				$i--;
			}
		}
	}
	
	sub get_files {
		local($dir) = "@_";
		push(@FILES,$dir); 
		# get the list of files in the current directory #
		opendir(DIR, $dir);
		local(@filenames) = grep(!/^\.\.?$/, readdir(DIR));
		closedir(DIR);
	
		for $tmp (@filenames) {
			$name = "$dir:$tmp";
			&get_files($name) if (-d $name);
	   	  	push(@FILES,$name) if (!-d $name);
	   	  	next;
		}
		return @FILES;
	}
	
	do END;

}

At 11:36 AM 2/20/98 -0600, Wade Williams wrote:
>At 12:42 PM 2/19/98 -0500, Chris Nandor wrote:
>>At 12.14 1998.02.19, Wade Williams wrote:
>>>Hmm, I must be doing something stupid:
>>>
>>>$webpath="Wade'sWorld:Web Pages:Carriers.19.2.1998:";
>>>$shortpath=$webpath;
>>>chop($shortpath);
>>>
>>>if (-e $shortpath)
>>>{
>>>    foreach(<$webpath*>)
>>>    {
>>>       unlink($_);
>>>    }
>>>    rmdir($shortpath);
>>>}
>>>mkdir($shortpath,0775);
>>>
>>>This code fails to remove any files from the directory specified by
>>>$webpath.  The path is correct, the directory exists, and the directory has
>>>files in it.  It simply evaluates the foreach line, and moves onto the
>>>rmdir line (which of course fails since files still exist).
>>


-- Jon S. Jaques     --    The Grove Hill Pages --
--                   Now on the PerlRing!                     --
--      http://www.grovehillsys.com/scripting      --
--              jjaques@grovehillsys.com                 --
--  Linux: Salvation from Bill, Steve and Marc!  --

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch