467,146 Members | 1,020 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

Removing directory using perl script

Hi I tried a simple script to remove the directory as below:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use File::Find;
  3. #finddepth(sub{rmdir},'.')
  4. system('rm -rf test3');
1. finddepth(sub{rmdir},'.') removed all the empty directories.
2. But I wanted to remove all the directories which are not empty. Hence I tried
Expand|Select|Wrap|Line Numbers
  1. system('rm -rf test3');
to remove all directories inside test3 directory and it works.

Please let me know whether there are any other elegant way of removing directories? Removing the directories takes more time (approximately 20 minutes) and hence is it advisable to fork the process and do?Thanks.
Sep 11 '08 #1
  • viewed: 10554
Share:
6 Replies
numberwhun
Expert Mod 2GB
Well, instead of invoking the system 'rm' command, you could use the rmdir function provided by Perl.

Regards,

Jeff
Sep 11 '08 #2
KevinADC
Expert 2GB
I think File::Path will delete empty and non-empty folders. How elegant it is though, I have no idea. You could traverse the directories and sub directories and delete all the files then delete the folders.
Sep 11 '08 #3
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. deldir("test"); # or deldir($ARGV[0]) to make it commandline
  3.  
  4. sub deldir {
  5.   my $dirtodel = pop;
  6.   my $sep = '\\'; #change this line to "/" on linux.
  7.   opendir(DIR, $dirtodel);
  8.   my @files = readdir(DIR);
  9.   closedir(DIR);
  10.  
  11.   @files = grep { !/^\.{1,2}/ } @files;
  12.   @files = map { $_ = "$dirtodel$sep$_"} @files;
  13.   @files = map { (-d $_)?deldir($_):unlink($_) } @files;
  14.  
  15.   rmdir($dirtodel);
  16. }
From URL REMOVED PER SITE POLICY
Nov 26 '09 #4
Expert Mod 512MB
That's a poor example, which you should not use.

Here's a cleaner, shorter, and more efficient method.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use File::Path qw(remove_tree);
  6.  
  7. remove_tree('test');
Or, if you're on a *nix system, you could do this:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. system('rm -rf test');
Nov 27 '09 #5
numberwhun
Expert Mod 2GB
Sweetypie,

First, I have removed your URL that you put into your post. We do not allow personally promotional URLs in the forums, thus, why I have removed it.

Second, please keep in mind the age of the posts that you are replying to. This one was over a year old.


Regards,

Jeff
Nov 27 '09 #6
Using system command is the worst way of doing it.
Although the use File::Path qw(remove_tree) is the best.

@jeff : oops ...
Dec 3 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Joe Cosby | last post: by
8 posts views Thread by Shabam | last post: by
3 posts views Thread by Alexandre H. Guerra | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.