Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

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

SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shragi

ASKER

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