Forum Moderators: coopster & phranque

Message Too Old, No Replies

Delete All Except . . .

         

jimijoe

7:19 am on Jan 15, 2006 (gmt 0)

10+ Year Member



Hi,

What's the code to delete all files & directories except files a.html, b.html & dir C?

Thanks,
Jimijoe

perl_diver

10:45 am on Jan 15, 2006 (gmt 0)

10+ Year Member



hehehe.... the good folks a devshed didn't give you the "code"? ;)

jimijoe

6:39 am on Feb 5, 2006 (gmt 0)

10+ Year Member



hehehe... so funny!
get there, take a look ^!^

perl_diver

9:09 am on Feb 5, 2006 (gmt 0)

10+ Year Member




Thank you all, specially Immortal1490 & KevinADC

you already thanked me mate :)

jimijoe

6:47 am on Feb 6, 2006 (gmt 0)

10+ Year Member



So, if this is you!
I thank you again ~

DrDoc

7:35 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, can we get someone to post the answer here as well, or should we consider this whole thread null, void, and pointless? :)

perl_diver

8:24 am on Feb 6, 2006 (gmt 0)

10+ Year Member



hehehe... sure we can


use strict;
use File::Path;

open (FH, 'path/to/a.html') or die "$!";
my $a_html = do {local $/; <FH>};
close FH;

open (FH, 'path/to/b.html') or die "$!";
my $b_html = do {local $/; <FH>};
close FH;

rmtree('c:/foo',0,0);

mkpath('c:/foo/C',0,0777);

open (FH, '> a.html') or die "$!";
print FH $a_html;
close FH;

open (FH, '> b.html') or die "$!";
print FH $b_html;
close FH;

I'm not sure if the other solution worked, maybe jimijoe could say if it did or not.


$path = '';
@contents = <$path**>;
foreach $in (@contents){
$fordeletion = $in;
$x = '';
$y = '';
($x,$y) = split('\.',$fordeletion);
$pass = 1;
if($in eq "$path\/a\.html"){$pass = 0;}
if($in eq "$path\/b\.html"){$pass = 0;}
if($in eq "$path\/C"){$pass = 0;}

if ($pass){
if($y eq ''){
rmdir($in);
}else{
unlink($in);
}
}
}

DrDoc

8:39 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or, you could always use
grep
with a regular expression (matching anything BUT the files you want to keep) and pipe that to
rm -rf

:)