Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the following tree structure .now i want to remove all files from the folder which starts with task.How can i do it using perl?

/home/rpasa/demo_checklist
`-- DEMO
    |-- top
    |   |-- DV
    |   |   |-- rev1
    |   |   |-- rev2
    |   |   |-- task.config
    |   |   `-- task.html
    |   |-- checklist
    |   |   |-- rev1
    |   |   |-- rev2
    |   |   |-- rev3
    |   |   |-- rev4
    |   |   |-- task.config
    |   |   `-- task.html


Expected output:

<pre>/home/rpasa/demo_checklist
`-- DEMO
    |-- top
    |   |-- DV
    |   |   |-- rev1
    |   |   |-- rev2
    |   |-- checklist
    |   |   |-- rev1
    |   |   |-- rev2
    |   |   |-- rev3
    |   |   |-- rev4


What I have tried:

my @logs = glob "task.*";
for (@logs) {
    next if $_ eq "task.*";
    unlink $_;
}
Posted
Updated 6-Mar-17 23:17pm

1 solution

Use my solution to your question How do I remove the subdirectories from the directory struture using Perl?[^] as starting point (untested EDIT: tested and fixed errors):
PERL
sub DelTaskFiles{
    my ($workdir) = shift;
    #print "Processing '$workdir'\n";
    my $mask = $workdir . '/*';
    # glob: Return list of filename expansions for search mask
    my @files = glob $mask;
    foreach $f (@files)
    {
        # Process sub directories
        if (-d $f)
        {
            DelTaskFiles($f);
        }
        else
        {
            my $filename = basename($f);
            if ($filename =~ /^task/)
            {
                #print "Removing $f\n";
                unlink $f;
            }
        }
    }
}

DelTaskFiles('/home/rpasa/demo_checklist');

}
General rule:
If you need to process subdirectories use a recursive function that processes the directories. In each directory iterate over the files and perform the required action for matching files.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900