We help IT Professionals succeed at work.
Get Started

Check file date before getting folder path

Last Modified: 2015-08-27
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.

Inputfile.txt is as below

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

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.

Watch Question
Most Valuable Expert 2014
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE