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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
monasConnect With a Mentor Commented:
foreach (@databack){
 next if ! -f $datadir.'/'.$_;
# you don't need this - these are directories :-)
# unless($_ eq '.' || $_ eq '..'){
  backup(...);
#}
}
0
 
mmcwAuthor Commented:
Edited text of question.
0
 
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
All Courses

From novice to tech pro — start learning today.