Avatar of shragi
shragi
Flag for India asked on

Check file date before getting folder path

Hi - I just asked the below question in another ticket, but i want some extension to it.

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.
that's my original question but now I want to print the output file only if the file contained in the folder is one day old.

Example:
Inputfile.txt is as below
C:\temp\f1
C:\temp\f2


f1 has one file that has modified date from 3 days go, but f2 has file that is created like an hour back.
so when I run the perl script I should only get
C:\temp\f1

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

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

my $inputFileName = 'C:\\temp\\input.txt';
my $outputFileName = 'C:\\temp\\output.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 '..' } readdir DIR) {
					print OUTFILE $dir, "\n";
	}
        closedir DIR;
}

close INFILE;
close OUTFILE;

exit $errorcode;
                                          

Open in new window


so now I would like to add condition something like below
$fileModifiedDate = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime(( stat filename )[9]));
            if($fileModifiedDate < currentDate -1)
            {
                              print OUTFILE $dir, "\n";
            }

can you guys please help me with the missing piece.

Thanks,
PerlScripting LanguagesShell Scripting

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes