Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting file that was last modified 1

Status
Not open for further replies.

Sidney5

Programmer
Nov 2, 2006
12
GB
Hi

I am failry new to Perl but picking it up. However I am stuck on one area. I need to find the file in a particular directory that was last modified. I have looked at stat 9 but still am not too sure as how to go through all the files, get their timestamp and then assign the file that was last modified to a variable.

I am sure its only a few line of code but could someone please give me some sample code to act as a skeleton.

Thanks
 
From one of my scripts:

$mtime = (stat($fileToStat))[9] or warn "Can't stat file $fileToStat: $!\n";
 
Try:

Code:
my $most_recent = 0;
my $filename = '';

opendir (DIR, "./dir/to/check");
foreach my $file (readdir(DIR)) {
   next if $file =~ /^\./; # skip . and ..
   my $modded = (stat("./dir/to/check/$file"))[9];

   # If this file was more recently updated than
   # the last $most_recent one...
   if ($modded > $most_recent) {
      $most_recent = $modded;
      $filename = $file; # keep this filename
   }
}
closedir (DIR);

print "$filename was the most recently updated file\n";

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Here is the entire checking bit I do:

Code:
while (defined($file = readdir(DIR))) {


 if (! -d $file){
                print "File $file is a regular file, testing it.\n";
                $fileToStat = "$srcDir/$file";
                $mtime = (stat($fileToStat))[9] or warn "Can't stat file $fileToStat: $!\n";
                if ($mtime eq undef) {
                        print "\$mtime is undefined, skipping $file.\n";
                        next;
                } else {
                        print "mtime (the last modify time since the epoch): $mtime\n";
 
Kirsle said:
next if $file =~ /^\./; # skip . and ..

This should really be:

Code:
next if $file =~ /^\.[COLOR=blue]\.?$[/color]/; # skip . and ..
 
Hi All

Thanks for your sampe code and your suggestions. A work colleague also showed me some sample code in a Perl script and. I liked it and chose to use it so have a look:

my $cashflow_file = get_latest_cashflow_file($srcRootCashFlow);

sub get_latest_cashflow_file {
my $dir = $_[0];
opendir(FILES, $dir);
@files = readdir(FILES);
closedir(FILES);

my @cashflow_files;
for(my $f=0; $f<@files; $f++){
if($files[$f] =~ /.*\.cashflows_today\.txt$/){
push(@cashflow_files,$files[$f]);
}
}

@cashflow_files= reverse sort by_lastmodified @cashflow_files;
return $cashflow_file[0];
}

sub by_lastmodified {
# sort files by last modified
my $d1 = (stat($a))[9];
my $d2 = (stat($b))[9];
return $d1 <=> $d2;
}

 
or maybe written simpler as:

Code:
my $cashflow_file = get_latest_cashflow_file($srcRootCashFlow);

sub get_latest_cashflow_file {
  my $dir = $_[0];
  my @cashflow_files = <$dir/*.cashflows_today.txt>;
  @cashflow_files = sort { (stat($b))[9] <=> (stat($a))[9]} @cashflow_files;
  return $cashflow_file[0];
}

- Kevin, perl coder unexceptional!
 
last line should be:

Code:
return $cashflow_file[COLOR=red]s[/color][0];

- Kevin, perl coder unexceptional!
 
or maybe a little more compact still:

Code:
my $cashflow_file = get_latest_cashflow_file($srcRootCashFlow);

sub get_latest_cashflow_file {
  (sort {(stat($b))[9] <=> (stat($a))[9]} <$_[0]/*.cashflows_today.txt>)[0];
}

Sorry ... couldn't help myself (-:
 
if you'r gonna trim it down that much you may as well go all the way:

Code:
my $cashflow_file = (sort {(stat($b))[9] <=> (stat($a))[9]} <$srcRootCashFlow/*.cashflows_today.txt>)[0];

[flowerface]

- Kevin, perl coder unexceptional!
 
Hi again

I have taken your ideas and coded the solutions in Perl. However, I get an error message when I try to retrive the lastest file from a directory. When I use these 2 lines (line numbers in brackets) :

228) my $latest_cashfile = (sort {(stat($b))[9] <=> (stat($a))[9]} <$srcRootCashFlow/*.cashflows_today.txt>)[0];
229) print "The cashflow file is: $latest_cashfile \n";

I get this error message:
Use of uninitialized value at ./PopulateLaserXLRunner_sverma line 229.

I get the same error message when I try to use some other code to get the latest file from a directory, eg get_latest_cashflow_file. I know that I am using the correct directory and I am searching for the correct files.

I feel that I am doing something fundamentally wrong, or maybe I have an error somewhere else in my code which is leading to this error. Any ideas?

Thanks
 
Code:
my  $latest_cashfile = (sort {(stat($b))[9] <=> (stat($a))[9]} <$srcRootCashFlow/*.cashflows_today.txt>)[0][b] || 'No data'[/b];

but then you need to figure out why no data is being generated.

- Kevin, perl coder unexceptional!
 
I was able to open up the last modified file but I am having difficulty in using pattern matching when reading from it. Pattern matching is not trival in Perl and I would appreciate your assistance. Here is how my file looks like that I have opened with CASH_FILE as the file handle:

PayDate;Book;TradeID;FlowType;PayCCY;PayAmt;ResetConv
11/27/2006;UNKNOWN;BILL009589;INT;EUR;498681;
11/27/2006;Ldn ASW Structured;IBLT009589;INT;EUR;-498681;

I am interestd in retriving the PayDate, Book, TradeID, FlowType, PayCCY and PayAmt fields which are all delimited with a semi colon. When I run my code, the 2nd line of the file gets successfully printed but the next line does not get printed. I get an error message saying "Use of uninitialized value at ..."

my $count = 0;
while(<CASH_FILE>)
{
my ($PayDate, $Book, $TradeID, $FlowType, $PayCCY, $PayAmt) = /^(\S+);(\S+);(\S+);(\S+);(\S+);(\S+);/;

if ($count > 0) # ignore the first line because it contains the headers.
{
$details="Cashflow#$PayAmt $PayCCY";
print "\n $PayDate, $Book, $TradeID, $FlowType, $PayCCY, $PayAmt \n";
}
}


Why does the 2nd line get printed correctly but not the 3rd line. I believe it is to do with my pattern matching. Can you spot something wrong?
Thanks
 
the spaces are the problem:

Ldn ASW Structured

you're regexp is looking for non-space characters (\S+). Using split(/;/) would be an easy solution:


Code:
my $undef = <CASH_FLOW>; #get rid of headers.
while(<CASH_FILE>)
{
   my ($PayDate, $Book, $TradeID, $FlowType, $PayCCY, $PayAmt) = split(/;/);
   $details="Cashflow#$PayAmt $PayCCY";
   print "\n $PayDate, $Book, $TradeID, $FlowType, $PayCCY, $PayAmt \n";
}

- Kevin, perl coder unexceptional!
 
Hi

I am trying to read from a file which has got 10 fields and I am interested when the fifth field (called adpNumber) has got a duplicate in it.

I want to get a list of the adpNumbers that are duplicates and I will use that list in an sql statement. Please can you tell me how to get a list of all of the possible duplicates?
I am guessing that perhaps there is a way of doing it by using regular expression or even with hash.
Here is a section of the perl code that I am using:

open (INFILE, $infile) or die "Can't open file $infile for reading";

my $headers = <INFILE>;

my $line;
while ($line = <INFILE>) {
$line =~ s/\r\n$/\n/;
if ($line =~ /^\s+$/) {
next; # skip blank lines
}
chomp($line);

my ($ccy, $bk, $issuer, $asset, $adpNumber, $posId, $markDate, $price,
$amtSettled, $amtTraded) = parseCSVLine($line);

$adpNumber =~ s/\s+//g;
$adpNumber = uc $adpNumber;
$ccy =~ s/\s+//g;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top