CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Can't remove directory using rmdir?

 



AlexPerl
Novice

Apr 16, 2014, 4:36 PM

Post #1 of 3 (5283 views)
Can't remove directory using rmdir? Can't Post

How to remove empty and non-empty directory? I use rmdir, but it didn't remove the directories. If I replace rmdir with remove_tree, the directories are removed but I get the annoying message: Can't cd to (/tmp/lvs/) svdb: No such file or directory at /home/me/codes line 154.

This is because of my inexperienced Perl coding. Is there a way to silent this message like in Linux?

Also, I have many directories to find and delete. I dont want to write dir1, dir2, dir3, .. dir250 in my code. Is there a way to code in Perl finding all the dir* directory; ie. if(-d $dir eq "dir*")?


Code
find(\&rm_dir, $arcstoreMDA); 
sub rm_dir
{
my $dir = $_;
if(-d $dir &&
$dir ne "." &&
$dir ne ".." &&
$dir eq "svdb" |
$dir eq "dir1" |
$dir eq "dir2" )
{
rmdir $File::Find::name;
if(-e $File::Find::name)
{
print "Directory still exists '$File::Find::name'\n";
}
else
{
print "Directory deleted '$File::Find::name'\n";
}
}
}



Laurent_R
Veteran / Moderator

Apr 17, 2014, 2:04 AM

Post #2 of 3 (5278 views)
Re: [AlexPerl] Can't remove directory using rmdir? [In reply to] Can't Post

I do not know what your directory tree looks like, but you probably need to first delete the files and then delete the directories from the bottom of the directory tree up.

It might probably be done with File::Find, but I am not sure how and don't have time now to investigate

You could write your own recursive depth-first traverse_dir function implementing the ideas above, something like this:


Code
sub traverse_dir { 
my $path = shift;
my @dir_entries = glob("$path/*");
foreach my $entry (@dir_entries) {
next if $entry eq "." or $entry eq ".."; # add your own conditions if any
unlink $entry and next if -f $entry;
traverse_dir ($entry) if -d $entry; # recurse down the tree
rmdir $entry; # the recursive call has now emptied everything underneath, the dir can be removed
}
}


Please, be very careful, I have not tested the above function. I have only tested the following one, which just prints what it would delete or remove if it were the real program and I can see that it is traversing the directory tree in the right order:


Code
use strict; 
use warnings;

die "\n Wrong syntax!! Usage: remove_dir.pl directory\n" unless @ARGV == 1;

traverse_dir (shift);

sub traverse_dir {
my $path = shift;
my @dir_entries = glob("$path/*");
foreach my $entry (@dir_entries) {
next if $entry eq "." or $entry eq ".."; # add your own conditions if any
print "I delete $entry\n" and next if -f $entry;
traverse_dir ($entry) if -d $entry; # recurse down the tree
print "I remove dir $entry\n"; # the recursive call has now emptied everything underneath, the dir can be removed
}
}


This is part of the output:


Code
I delete ./nested_loops.pl 
I delete ./new_dir/a.pl
I delete ./new_dir/data.txt
I remove dir ./new_dir
I delete ./number_guess.pl
I delete ./ol.pl
I delete ./Old_PL_programs/24.pl
I delete ./Old_PL_programs/2tab.pl
I delete ./Old_PL_programs/aa.pl
I delete ./Old_PL_programs/football.pl
I delete ./Old_PL_programs/format.pl
I delete ./Old_PL_programs/ip.pl
I delete ./Old_PL_programs/qpar.pl
I delete ./Old_PL_programs/regex-test.pl
I delete ./Old_PL_programs/spell.pl
I delete ./Old_PL_programs/test3.pl
I delete ./Old_PL_programs/whitespace.pl
I delete ./Old_PL_programs/xml.pl
I delete ./Old_PL_programs/zip_read.pl
I remove dir ./Old_PL_programs


As you can, see file deletion and directory removing messages are listed in the right order for the actual deletions to be possible if it were the real program.

Again, be very very careful when testing (and using) such a program, a small mistake may lead to a disaster, and it might wipe out your entire disk if you don't supply the right argument.


AlexPerl
Novice

Apr 17, 2014, 9:23 AM

Post #3 of 3 (5262 views)
Re: [Laurent_R] Can't remove directory using rmdir? [In reply to] Can't Post

Thank you for the code Laurent_R. I will test it in a small directory tree in a few days. I need to dissect and understand your codes before using it. I dont want to delete anything important at work, I would be in BIG trouble.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives