Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am unable to read the files recursively inside the directories.Here $dir1 is the directory which has the file names with task recursively.I want to remove those files which starts with task recursively.How can i do it?

Error:

Use of uninitialized value in pattern match (m//) at ./generate_dcms_html.pl


My input_Directory struture:

`-- added
   |-- add.txt
   `-- added1
       |--action
       |   |-- action.txt
       |   `-- task1
       |   |    `-- task1.html
       |   `-- task2
       |       `-- task1.html

       `-- add.html


Expected:

`-- added
   |-- add.txt
   `-- added1
       |--action
       |   |-- action.txt
       |   `-- task2
       |       `-- task2.html

       `-- add.html


What I have tried:

sub ProcessDirectory1{
    my ($workdir1) = shift;
 	unless (-d $workdir1)
    {
        warn "argument to DelTask is not a directory.\n";
        return;
    }
    my @dirs1 =  glob "$workdir1/*";

    foreach my $f (@dirs1)
    {
    	my $dirs1=basename($f);
    	{
        if (my $dirs1 =~ /^task/)
        {
          my $filemask1 = $f.'/*';
          print $filemask1;
          unlink glob $filemask1;
          unlink $f;          
        }

        
    }
}
}
ProcessDirectory1($output_dir);
Posted
Updated 18-Apr-17 23:15pm
v4
Comments
Richard MacCutchan 18-Apr-17 2:58am    
Yet another unclear/incomplete question. I explained yesterday (and previously) the information we need to see, but you still do not provide it.
gps sana 19-Apr-17 4:46am    
I had showed you the information.Please look into it

1 solution

The regex must be applied to an existing variable but you are trying to create a new one using my and there is an unnecessary brace:
PERL
my $dirs1=basename($f);
{
    if (my $dirs1 =~ /^task/)
So it should be:
PERL
my $dirs1=basename($f);
if ($dirs1 =~ /^task/)
# ...
# Remove closing brace too
You should also not use the name of an already existing variable (dirs1 is already used for an array). While this is possible it is bad style and makes finding errors difficult. This is better:
PERL
my $file_name=basename($f);
if ($file_name =~ /^task/)

[EDIT]
If you want to process directories recursively you have to call ProcessDirectory1 for sub directories:
PERL
foreach my $f (@dirs1)
{
    if (-d $f)
    {
        ProcessDirectory1($f);
    }
    else
    {
        my $filename=basename($f);
        # ...
    }
}
[/EDIT]
 
Share this answer
 
v2
Comments
gps sana 19-Apr-17 0:05am    
sub ProcessDirectory1{

my ($workdir) = shift;

my @dirs1 = glob "$workdir/*";

foreach my $f (@dirs1) {
chomp ($f);
my $file_name=basename($f);
if ($file_name =~ /^task/)
{
print "if";
my $filemask1 = $f.'/*';
print $filemask1;

unlink glob $filemask1;
unlink $f;
}

}

}
ProcessDirectory1($output_dir);


Still i have the problems print statements are not working.
Jochen Arndt 19-Apr-17 2:42am    
The print statements are working.
But there is probably no file name beginning with "task" so that no print statement is reached in your code.
gps sana 19-Apr-17 4:42am    
but i have files under the sub directories then why it is not reading those files.
Jochen Arndt 19-Apr-17 4:54am    
The solution is simple:

You have no recursion!
You have to call ProcessDirectory1() again for each directory found in the actually processed directory.

I will update my answer.

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