Find files in dir

Hello,

I want to push all the filenames into @databack, but not the sub directories!!
When I use the code downhere it will also push the sub dir into @databack.
And also . and ..

opendir(D, "$datadir") or my_die("Error in subroutine backup: can't open $datadir", $!);
push(@databack,readdir(D));
closedir(D);

How to make the script so that it will not do so and only pushes the files into @databack!

I use this complete script:

opendir(D, "$datadir") or my_die("Error in subroutine backup: can't open $datadir", $!);
push(@databack,readdir(D));
closedir(D);

foreach (@databack) {
unless ($_ eq "." || $_ eq "..") {
backup("$datadir/$_","$datadir/Backup/$_");
}
}

-backup is a subscriopt that backups the files to a backupdir!

greetings Michel
mmcwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmcwAuthor Commented:
Edited text of question.
0
monasCommented:
foreach (@databack){
 next if ! -f $datadir.'/'.$_;
# you don't need this - these are directories :-)
# unless($_ eq '.' || $_ eq '..'){
  backup(...);
#}
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmcwAuthor Commented:
Thank you for your answer!

I have another little question:
Could you tell me how to use the command rmdir onlky when the dir is empty!!

rmdir "$datadir/Backup";

When I try to delete the dir $datadir/Backup when it is not empty I will get an error!!

Do I have to us something like this:

if (-d "$datadir/Backup") {
  rmdir "$datadir/Backup";
}

I should like it a lot if you could answer this little question to so I can use it in the part above!!
0
monasCommented:
Well,

      You can directly delete directory only when it is empty. So you have two options:

1) find out what files/subdirs are inside; delete them; delete subdir;

in perl

require "finddepth.pl";
sub wanted{
  if (-d $name){ rmdir $name;}
  else { unlink $name;}
}
finddepth("$datadir/Backup");

2) use unix command "rm -rf dir_to_delete" (but be carefull - it WILL NOT ask you)

in perl
system ("/bin/rm -rf $datadir/Backup");
0
mmcwAuthor Commented:
But how to check if an directory is empty???

I only want to delete the dir if it is empty!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.