LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-31-2009, 04:19 AM   #1
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Rep: Reputation: 15
perl script for latest file from folder


Hi
i want to get the latest file name from a directory, how can i do with perl script. In other words, how to sort the contents of a folder on the basis of time and capture the latest file using perl script, Thanks in advance
 
Old 07-31-2009, 08:05 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This smells like homework, but if you show us what you've tried so far, perhaps someone can point out your errors. Post code in [CODE] tags please.
--- rod.
 
Old 08-03-2009, 03:43 AM   #3
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
This is not homework, i'm trying to do some patchup work, which my colleague has left in middle. That is why all these problems. As Perl is new to me, i'm practising with small programs and once i get them, i'm incorporating them into original project code. Now the thing is, i need to read latest file from a folder, i could write the script to read the first line of the file, so if i get names of the files in a sorted order, the first file would be my latest file, so i want to know, how can i put them in order using perl...
Can you suggest me something...

Code:
use warnings;

open(MYFILE, 'filelist.txt');
$line = 0;
$l = 1;
$c = 1;

 while (<MYFILE>)
 {
  if ($c == $l)
   {
     print "$_\n";
     $line = $_;
    }  
   $c++;
 }

close (MYFILE);
 
Old 08-03-2009, 11:27 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, the relevant weapons in your perl arsenal should include opendir(), stat(), & sort. These functions allow you to open a directory and read its contents, get information about each of the files in the directory, and then to sort the contents according to your criteria, where the criteria are all of the elements of the data returned by stat(). This should get you started:
Code:
#! /usr/bin/perl -w
#
#   LQnanda22.pl
#   Usage: LQnanda22.pl  dirname
#
#   Print a directory, sorted by date/time
#
use Cwd;

    my $cwd = cwd();            # Save current working dir.
    chdir( $ARGV[0] );          # Make specified dir the working dir
    
    #   Open and slurp the whole working dir
    opendir( DIR, "." ) || die "Cannot open dir $ARGV[0] : $!\n";
    my @dir = readdir(DIR);
    closedir( DIR );
    
    # Print, sorted by 'mtime', the whole contents of working dir
    foreach my $file ( sort byDate @dir ){
        print "$file\n";
    }
    
    # Return to Kansas...
    chdir( $cwd );
    exit 0;
    
#
#   Helper for sort. Compares two files' mtimes.
#    
sub byDate(){
    my @stat1 = stat( $a );
    my @stat2 = stat( $b );
    return $stat1[9] cmp $stat2[9];
}
Your method seems to require an input file that already has the directory content sorted, which is fine, but requires a system() call, which I prefer to avoid when there are perl functions that already do the same work.

--- rod.
 
Old 08-03-2009, 08:31 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Alternate variation: http://snipplr.com/view/3441/sort-fi...d-or-accessed/
 
Old 08-05-2009, 08:54 AM   #6
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Hi rod,
Thanks a lot for your script. Its working fine. i just modified it to read only .txt files. But i've one doubt, why is it reading only from current directory, even though i mentioned the directory from which it has to read files, its not taking. Can you please tell me where i've done mistake?
Code:
opendir dir, "D:\Softwares\Perl";

@fil = glob "*.txt";

       foreach my $f (sort byDate @fil)

       {
          print "$f \n";
       
        }

sub byDate()
{
    my @stat1 = stat( $a );
    my @stat2 = stat( $b );
    return $stat2[9] cmp $stat1[9];
}
I understood that i'm not using "dir" variable anywhere again, then where should i give path?

Last edited by nanda22; 08-05-2009 at 09:08 AM.
 
Old 08-05-2009, 10:21 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You've opened a directory for reading, using opendir(), but then failed to read from the open filehandle, instead reading from the current working directory using glob. To read from the open filehandle, use readdir(). In your code, your filespec does not specify a directory, so the current working directory is used.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Run Script on all Files in folder on webserver baddah Programming 2 07-10-2008 03:26 PM
Help create perl script for delete file/folder sportivo888 Linux - Newbie 5 12-05-2007 11:33 PM
create a folder/file within a folder using perl indu.a Programming 2 03-07-2007 10:09 PM
help: script to ftp latest file jayasheela Linux - General 0 12-20-2006 04:17 AM
perl script help required to create users and changing the owner to pericular folder sridhar11 Programming 2 10-25-2005 02:16 PM


All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration