Removing directory using perl script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    Removing directory using perl script

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

    Code:
    #!/usr/bin/perl -w
    use File::Find;
    #finddepth(sub{rmdir},'.')
    system('rm -rf test3');
    1. finddepth(sub{r mdir},'.') removed all the empty directories.
    2. But I wanted to remove all the directories which are not empty. Hence I tried
    Code:
    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.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Well, instead of invoking the system 'rm' command, you could use the rmdir function provided by Perl.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      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.

      Comment

      • sweetypie
        New Member
        • Nov 2009
        • 3

        #4
        Script to recursively delete

        Code:
        #!/usr/bin/perl
        deldir("test"); # or deldir($ARGV[0]) to make it commandline
         
        sub deldir {
          my $dirtodel = pop;
          my $sep = '\\'; #change this line to "/" on linux.
          opendir(DIR, $dirtodel);
          my @files = readdir(DIR);
          closedir(DIR);
         
          @files = grep { !/^\.{1,2}/ } @files;
          @files = map { $_ = "$dirtodel$sep$_"} @files;
          @files = map { (-d $_)?deldir($_):unlink($_) } @files;
         
          rmdir($dirtodel);
        }
        From URL REMOVED PER SITE POLICY
        Last edited by numberwhun; Nov 27 '09, 01:15 PM. Reason: Personally promoting URLs are not allowed in the forums.

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          That's a poor example, which you should not use.

          Here's a cleaner, shorter, and more efficient method.
          Code:
          #!/usr/bin/perl
          
          use strict;
          use warnings;
          use File::Path qw(remove_tree);
          
          remove_tree('test');
          Or, if you're on a *nix system, you could do this:
          Code:
          #!/usr/bin/perl
          
          use strict;
          use warnings;
          
          system('rm -rf test');

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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

            Comment

            • sweetypie
              New Member
              • Nov 2009
              • 3

              #7
              Using system command is the worst way of doing it.
              Although the use File::Path qw(remove_tree) is the best.

              @jeff : oops ...

              Comment

              Working...