Sorting files based on the last modified time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pramodkh
    New Member
    • Nov 2007
    • 23

    Sorting files based on the last modified time

    Hi All,

    I have a remote directory wherein some files will be generated. I am writting a PERL script in windows to get the latest file created in that directory.How do i achieve this?

    This is what i have tried so far...
    Code:
    use FindBin;
    use strict;
    
    my $ScriptPath = $FindBin::Bin;  
    print "\n Script Path =  $ScriptPath ";
    my @files = "";
    ### remote directory
    my $dirPath = "D:\\Test\\TestMaintenance";
    opendir(DIRHANDLE, "$dirPath") or return("\n Can not open $dirPath \n $!");
    while ( defined (my $filename = readdir(DIRHANDLE)) ){
    	if($filename =~ m/TestMaintenance/){
    		print "\n$filename";
    		push(@files, $filename);
    	}
    }
    close DIRHANDLE;
    looks like opendir gives random file list. Its not in a sorted order. How do i sort files in a directory based on the file modification time?

    Thanks in advance.

    Regards
    Pramod
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by pramodkh
    Hi All,

    I have a remote directory wherein some files will be generated. I am writting a PERL script in windows to get the latest file created in that directory.How do i achieve this?

    This is what i have tried so far...
    Code:
    use FindBin;
    use strict;
    
    my $ScriptPath = $FindBin::Bin;  
    print "\n Script Path =  $ScriptPath ";
    my @files = "";
    ### remote directory
    my $dirPath = "D:\\Test\\TestMaintenance";
    opendir(DIRHANDLE, "$dirPath") or return("\n Can not open $dirPath \n $!");
    while ( defined (my $filename = readdir(DIRHANDLE)) ){
    	if($filename =~ m/TestMaintenance/){
    		print "\n$filename";
    		push(@files, $filename);
    	}
    }
    close DIRHANDLE;
    looks like opendir gives random file list. Its not in a sorted order. How do i sort files in a directory based on the file modification time?

    Thanks in advance.

    Regards
    Pramod
    Well, you can use Perl's 'sort()' function like this:

    [code=perl]
    sort{ -M $b <=> -M $a }
    [/code]

    That will sort in reverse order by time. You could assign that to an array and your highest element is the newest and the first element is the oldest.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      also replied to on devshed.

      Comment

      • pramodkh
        New Member
        • Nov 2007
        • 23

        #4
        Thanks Jeffin and Kevin.
        I am working on this...
        Facing some problems in accessing remote server, from where i have to read the files. i will look into this and get back to you.

        You both always provide optimum solutions within short time:-)

        Thanks again.

        Pramod

        Comment

        Working...