perl script to check whether folder contains any files

Hi - I have a requirement where I want to find whether any files are in the given folder.
So I listed all the folder paths in a text file and want to send that file as input to perl script
now perl script reads each line and gets each folder path and should check whether it contains any files
if it contains any files then it should print that folder path into the output file.

input file like below

use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use Time::Piece;

my $helpme = 0;
my $man = 0;

my $inputFileName = 'C:\\temp\\folderlist.txt';
my $outputFileName = 'C:\\temp\\outputfolderlist.txt';

my $errorcode = 0;
my $DEBUG=0;

if(exists $ENV{DEBUG}) {
	$DEBUG = ($ENV{DEBUG} eq "") ? 0 : $ENV{DEBUG};

GetOptions('help' => \$helpme, 'man' => \$man, 'infile=s' => \$inputFileName, 'outfile=s' => \$outputFileName) or pod2usage(2);

pod2usage(1) if $helpme;
pod2usage(-verbose => 2) if $man;

die 'No input file name specified!' unless $inputFileName;
die 'No output file name specified!' unless $outputFileName;

open(INFILE, '<', $inputFileName) or die "Could not open input file: $!";
open(OUTFILE, '>', $outputFileName) or die "Could not open/create output file: $!";

while(<INFILE>) {

close INFILE;
close OUTFILE;

exit $errorcode;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

You can use the glob function to check for files.

Something like this:
while( my $dir = <INFILE>) {
	chomp $dir;
	if (glob "$dir/*") {
            # print dir path to your file.

Open in new window

You can also use the File::Glob module.
I'm trying to think of a way to do this that uses minimum resources...thinking about opendir and readdir. Globbing certainly works, but at what cost? And does it do the right thing for hidden files (names beginning with .)?

Perhaps better would be to have the script that prepares the input list to simply list all the file pathnames; that would make the answer to this question just a matter of text processing rather than an exercise for file system operations.
The glob function will return hidden files as well.
if (glob "$dir/. $dir/*") {

Open in new window

Since it's being called in scalar context within a conditional, it will be very efficient because it's only returning 1 value which is either undef or the first directory entry.

One caveat with globing hidden files is that it returns the . and .. dirs which would need to be taken into account.  One way to handle it would be to filter the glob via a grep statement.
If you wanted to use readdir, it would look like this...
while( my $dir = <INFILE>) {
	chomp $dir;
        opendir DIR, $dir or die "could not open dir $dir: $!";
        if (grep { $_ ne '.' and $_ ne '..' } readdir DIR) {
            # print dir path to your file.
        closedir DIR;

Open in new window

The grep above could also be used with the glob as FishMonger suggested.

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
shragiAuthor Commented:
Thanks FishMonger and wilcoxon.

I used the wilcoxon script to get the result that I want, I have another question but I will open another ticket for that.

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

From novice to tech pro — start learning today.