Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2

    Thumbs up Removing directory


    Hi All,

    Suppose my directory structure is like:

    Top(Top directory name): abc,efg,xyz (3 directories, abc,efg,xyz in Top directory)
    abc: a.txt (one file in Top/abc/)
    efg: p.txt (one file in Top/efg/)
    xyz: g.cpp, abc (one file and one directory,abc, in Top/xyz/)
    abc: pqr (one directory in Top/xyz/abc/)
    pqr: j.sv (one file in /Top/xyz/abc/pqr/)
    ___________________________________________________________

    Now using command:
    perl <filename.pl> Top
    I want to delete all the "abc" directories in "Top" directory, including all the files and directories inside "abc".
    ___________________________________________________________

    Can any one please guide me?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    The best is probably to use the File::Path CPAN module, which can remove whole directory trees.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Laurent_R
    The best is probably to use the File::Path CPAN module, which can remove whole directory trees.
    Hi Laurent_R,

    Can you please elaborate?
    I don't know how to access it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    CPAN (Comprehensive Perl Archive Network) is a network of weksites that constitute a repository for tens of thousands of additional modules written and shared by thousands of people around the world to do an incredibe variety of things.

    The module I am talking about is there :

    http://search.cpan.org/~dland/File-Path-2.09/Path.pm

    You'll need to install this module and follow the instructions provided on this link.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Laurent_R
    CPAN (Comprehensive Perl Archive Network) is a network of weksites that constitute a repository for tens of thousands of additional modules written and shared by thousands of people around the world to do an incredibe variety of things.

    The module I am talking about is there :

    http://search.cpan.org/~dland/File-Path-2.09/Path.pm

    You'll need to install this module and follow the instructions provided on this link.
    Hi Laurent_R,

    Thanks for the replying.
    I have one more query,
    How to find directory with its name?
    i.e. suppose my directory name is "abc"
    So how to find all the directories with the same name???
    ___________________________________________________
    I have tried this:
    $name: "abc";
    if (-d $first)
    {
    @files2 = grep { /$name/ }$first;
    } #first is pointing to current directory
    ______________________________________ _____________
    But this command also matching the directories with name "abc",abc_99,"abc24" etc..

    If you can please help me?
  10. #6
  11. Recovering Intellectual
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2006
    Location
    Orange County, CA
    Posts
    1,306
    Rep Power
    785
    your regular expression needs to match correctly. You need directories beginning and ending as abc. for example:

    $directory =~ m/^abc$/g;
    Bugs that go away by themselves come back by themselves
    Beware - your loyalty will not be rewarded
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    Hi Bonny,

    I am not entirely sure what you are trying to do, but I think your approach is wrong.

    Perl Code:
    @files2 = grep { /$name/ }$first;


    You probably don't really understand the grep function. The argument of a grep command is an array or a list of items, not a scalar. I do not know what the content of $first is, it might work depending of the exact content, but my feeling is that this is not right.

    If you are trying to list the content of a directory, you should rather use either the readdir function or, even better in most cases, the glob function, which uses the system pattern-matching and has the advantage of returning the full relative path, rather than simply the file names.

    For example, on my system:

    Perl Code:
    my @dir =  glob ("./Bioperl/*.*");


    Now, the @dir array contains 2 items :

    Code:
    0  './Bioperl/BioPerl-1.6.901'
    1  './Bioperl/BioPerl-1.6.901.tar'
    And if i do this:

    Perl Code:
    @dir =  glob ("./Bioperl/*/*.*");


    I now have @dir containing the following:

    Code:
    0  './Bioperl/BioPerl-1.6.901/BioPerl.pm'
    1  './Bioperl/BioPerl-1.6.901/Build.PL'
    2  './Bioperl/BioPerl-1.6.901/INSTALL.SKIP'
    3  './Bioperl/BioPerl-1.6.901/INSTALL.WIN'
    4  './Bioperl/BioPerl-1.6.901/META.json'
    5  './Bioperl/BioPerl-1.6.901/META.yml'
    I hope you get the idea.

    This is much easier to use in 99% of the cases.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Hi Matt1776 and Laurent_R,

    Thanks for replying.

    @Matt1776,

    I tried but it is not working in a way I want. I am going through each and every directory from the top directory. $firsrt is the current directory. So I want to see if $first(current directory's name) and $name are same or not and if yes I want to store them in an array. Can you please help me in another way?

    @Laurent_R,

    What I am saying is, I want to match directory's name.
    Directories with name "abc" only, not "abc8", "abc_" etc
    $first is one directory at a time, which is in loop.
    But, I want your help in only the matching directory's name.
    Here ,

    $name="abc"
    @files2= grep { /$name/ }$first ;

    $name is the directory name(for matching purpose).
    So in the second line it will match with $first, if $first is also "abc" directory or not and if yes then add it to @files array.
    But the problem is, in @files2 it is also adding the directory with name "abc_99" "abcd" etc

    If possible please help me if you can.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    Then don't use a regular expression but test for equality:

    Perl Code:
    @files2= grep { $_ eq $name } $first ;


    Or use the regex suggested by Matt:

    Perl Code:
    @files2= grep { /^$name$/ } $first ;
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Laurent_R
    Then don't use a regular expression but test for equality:

    Perl Code:
    @files2= grep { $_ eq $name } $first ;


    Or use the regex suggested by Matt:

    Perl Code:
    @files2= grep { /^$name$/ } $first ;
    Hi Laurent_R,

    Still I am facing some issues.
    Can you please help with this code, for the same problem somehow I am not getting the output.
    For the give top directory I want to go all the sub-directories of the directory and want to remove the directory with name "abc" only.

    #!/usr/bin/perl
    $name = "abc";
    sub readdirR{
    my @FDList = @_;

    my $first = shift @FDList;

    if (!$first){
    return ();
    }
    elsif (-f $first){
    return ($first, readdirR (@FDList));
    }
    elsif (-d $first){
    #my $Res = system ("rm -rf $name");
    #print "Result $Res";
    @files2 = grep { $_ eq $name } $first;

    opendir DIR, $first || warn "Cannot open directory $first: $!";
    my @files = readdir DIR ;
    closedir DIR;
    @files = grep {$_ !~ /^[.]{1,2}$/} @files;
    @files = map {"$first/$_"} @files;
    #print @files2;
    for my $f (@files2)
    {
    system "rm -rf $f";
    }
    return ($first, readdirR (@files), readdirR(@FDList));
    }
    }

    ####main program###########
    $" = "\n";
    my @FDToRead = @ARGV;
    if (!@FDToRead)
    {@FDToRead = ".";}

    my @allFiles = readdirR (@FDToRead);
    @allFiles = sort @allFiles;
    print "@allFiles\n\n\n\n\n";
    print "@files2";
    ___________________________________________________
    Can you help me?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    I told you that it did not make sense to use grep on a scalar, but you basically answered that you were not interested with my comments on your code and that you wanted to match only "abc", not "abc_something". So I told you how to do that, but now you figure out that there are other issues and ask for help. Why not listening in the first place?

    I can see a number of other issues in your code, but I think that the first thing you should do is to add the following lines at the top of your program:

    Perl Code:
    use strict;
    use warnings;


    You will probably get a number of errors and/or warnings that will tell you where some mistakes are. Once you done rthat and obtained a clean compile with the two use pgramas above, please come back and tell us if it still does not work as you wish.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Laurent_R
    I told you that it did not make sense to use grep on a scalar, but you basically answered that you were not interested with my comments on your code and that you wanted to match only "abc", not "abc_something". So I told you how to do that, but now you figure out that there are other issues and ask for help. Why not listening in the first place?

    I can see a number of other issues in your code, but I think that the first thing you should do is to add the following lines at the top of your program:

    Perl Code:
    use strict;
    use warnings;


    You will probably get a number of errors and/or warnings that will tell you where some mistakes are. Once you done rthat and obtained a clean compile with the two use pgramas above, please come back and tell us if it still does not work as you wish.
    Hi Laurent_R,
    I am sorry for that and so now I have found the issue.
    So let me try in a way you have asked.
    And if I found any issue I will get back to you.
    Sorry again and Thank you.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,954
    Rep Power
    1225
    Use File::Find::Rule to get the list/array of matching directories. That could be done in 1 line of code.

    Then loop over that array using the methods in File::Path to delete each directory tree. That can be done in 1 to 3 lines.

    File::Find::Rule - Alternative interface to File::Find

    File::Path - Create or remove directory trees

    You could combine those 2 steps and accomplish everything you need in 1 line of code.
    Last edited by FishMonger; April 19th, 2013 at 09:35 AM.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by FishMonger
    Use File::Find::Rule to get the list/array of matching directories. That could be done in 1 line of code.

    Then loop over that array using the methods in File::Path to delete each directory tree. That can be done in 1 to 3 lines.

    File::Find::Rule - Alternative interface to File::Find

    File::Path - Create or remove directory trees

    You could combine those 2 steps and accomplish everything you need in 1 line of code.
    Hi FishMonger,

    Thanks for your reply and help.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Hi All,

    Can you please help me in below give regex?
    Actually I am not good at regex.
    here, @files has the list of all the directories and files.
    and $cur_file performs the shifting operation from @files.
    ___________________________________________________
    @files = grep {$_ !~ /^[.]{1,2}$/} @files;
    @files = map {"$cur_file/$_"} @files;


    Can you tell me what actually performs?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo