Check only file date not subfolder date before getting folder path

Hi - I had the below script that checks each folder listed in the input file for any text file that is older than one day.
This program works but failed at one case, if the folder contains sub folder and if that sub folder is modified like 2 days back then the output of the program includes the folder that contains this sub folder.

Example:
The input file is like below
C:\\temp\\f1
C:\\temp\\f2
C:\\temp\\f3
C:\\temp\\f4

Now f1 contains no files and folders
f2 contains one text file with last modified date as two days ago
f3 contains one text file but it was created like an hour back
f4 contains one sub folder and one text file, the text file was created like an hour back but the sub folder has files from two days back so sub folder file modification date is two days old.

Now when I run the program I just need to see
C:\\temp\\f2

but the program below gives output as below
C:\\temp\\f2
C:\\temp\\f4

the reason is the sub folder within f4, so how can i fix it.

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

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

my $inputFileName = 'C:\\temp\\tempfold.txt';
my $outputFileName = 'C:\\temp\\test.txt';
my $fileModifiedDate = '';
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( my $dir = <INFILE>) {
	chomp $dir;
	opendir DIR, $dir or die "could not open dir $dir: $!";
	if (grep { $_ ne '.' and $_ ne '..' and  -M "$dir/$_" > 1 } readdir DIR) {
		print OUTFILE $dir, "\n";
	}
        closedir DIR;
}

close INFILE;
close OUTFILE;

exit $errorcode;
                                          

Open in new window

shragiAsked:
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.

FishMongerCommented:
If you want to exclude sub directories, then add that as part of the if conditional.

if (grep { $_ ne '.' and $_ ne '..'  and not -d "$dir/$_" and -M "$dir/$_" > 1 } readdir DIR) {
0
ozoCommented:
Given the  not -d "$dir/$_", the $_ ne '.' and $_ ne '..' becomes unnecessary.
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
shragiAuthor Commented:
Thanks FishMonger and OZO - the solution worked.
I have another question - what if I want to just check for a specific sub folder.
let us say if the control is in f4 and
1) how can I search for a subfolder named "Archive"
2) now if i found the "archvie" how can I get the date of most recent file in that archive folder,
i mean the archive contains lot of file but i am interested in the last file that is dropped in that archvie.

Thanks
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.