PDA

View Full Version : Delete non empty folders


NiteOwl
09-02-2006, 02:29 AM
Hello,
I am looking for a simple script to delete non empty folders.
Remove Directory and File(s)


Example i have a folder named 8122
i would like to delete it and all files and folders in it.

8122/tn/with several files
8122/ss/with several files
8122/html/with several files

Thanks for looking.

FishMonger
09-02-2006, 03:48 AM
http://search.cpan.org/~dmuey/File-Copy-Recursive-0.25/Recursive.pm#pathrm()

NiteOwl
09-02-2006, 04:11 AM
thanks for the linkfish monger
not sure that will help


what i have just written after more searches is this:



#!/perl/bin/perl


$document_root = "C:/Documents and Settings/James/My Documents/My Website/cgi-bin/test2";

$rmdir = "dummy";
$tmpdir = "$document_root/$rmdir";


print "Content-type: text/html\n\n";

use File::Path;
rmtree([$tmpdir], 0, 1) or $status = "Fail";

if ($status eq "Fail") {
$status = "Fail ($!)";
print "Removed $tmpdir $status</I>";
} else {
$status = "Success";
print "Removed $tmpdir $status</I>";
}




Is this acceptable code?
What checks can i do first?

FishMonger
09-02-2006, 05:27 AM
Yea, I forgot about that module and it's probably a better approach, but you need to be cautious. According to the docs, it will also delete the root dir, which in your script is $document_root = "C:/Documents and Settings/James/My Documents/My Website/cgi-bin/test2";

Similarly, the rmtree function provides a convenient way to delete a subtree from the directory structure, much like the Unix command rm -r. rmtree takes three arguments:

* the root of the subtree to delete, or a reference to a list of roots. All of the files and directories below each root, as well as the roots themselves, will be deleted.
However, in my test, it didn't remove the root path.

KevinADC
09-02-2006, 06:52 AM
thats funny, it should remove the root directory.

KevinADC
09-02-2006, 06:59 AM
Is this acceptable code?
What checks can i do first?


the code you used is fine, but it could be simplified:

$document_root = "C:/Documents and Settings/James/My Documents/My Website/cgi-bin/test2";
$rmdir = "dummy";
$tmpdir = "$document_root/$rmdir";

use File::Path;
$status = rmtree($tmpdir, 0, 1) ? "Remove $tmpdir Success" : "Remove $tmpdir Failed ($!)";
print $status;

NiteOwl
09-02-2006, 07:21 AM
<laughs>
Yes, it Deleted the root "test Folder" if $rmdir was blank
Also, it deleted it if $rmdir contained a "." or a ".."
So i added some checks.

This is what i have at the moment.
I want to do some checks considering what "rmtree" does.

How do i search a string to see if it contains a "." at all
if ($tmpbasename =~ /\.\./ does not do it.


Thanks to everyone
:thumbsup:




#!/perl/bin/perl


$document_root = "C:/Documents and Settings/James/My Documents/My Website/cgi-bin/test2/dummy";

$rmdir = "txt";
$tmpdir = "$document_root/$rmdir";
$**** = "$tmpdir/****";

print "Content-type: text/html\n\n";


use File::Basename;

if($rmdir =~ /./){ print "period found" }

$tmpbasename = basename($tmpdir);
if ($tmpbasename =~ /\.\./ || $tmpbasename eq ".") {
$status = "Fail (Invalid argument) - Folder Value = $rmdir";
print $status;
}
else {



use File::Path;


if ( $rmdir ne "") {
if (-e "$tmpdir")
{
print "Deleted - $rmdir \n";

rmtree([$tmpdir], 0, 1) or $status = "Fail";
if ($status eq "Fail") {
$status = "Fail ($!)";
print "Removed $tmpdir $status</I>";
} else {
$status = "Success";
print "Removed $tmpdir $status</I>";
}
}
else { print "Folder $rmdir - Does Not Exist \n";
}

} else { print "No Forder Selected \n"; }



}