#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    46
    Rep Power
    9

    Sort Directory and list files based on date and time


    hi all,

    I have to look for a directory and filter all CSV files. Then sort those based on the time they were created. I need to get the latest file then for further processing. I tried with 'sort', which does sorts, but sorts for the file name instead.

    your great ideas are welcome.

    Code:
    @files = get_CSV_Files($folderName);
    foreach (@files) {
    	print "\n$_";
    }
    
    sub get_CSV_Files {
       my $dir = shift;
       my $dh = DirHandle->new($dir)   or $val=3; #die "can't opendir $dir: $!";
       return sort                     # sort pathnames
              grep {    -f     }       # choose only "plain" files
              map  { "$dir$_" }        # create full paths
              grep {  m/.csv/i   }     # filter csv files
              $dh->read();             # read all entries
    }
    Thanks
    sath.
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    861
    you can create a hash where the keys are the filenames and the creation date is the value, then sort the hash by the values.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    46
    Rep Power
    9
    Originally Posted by KevinADC
    you can create a hash where the keys are the filenames and the creation date is the value, then sort the hash by the values.
    Good idea, Kevin
    I will try it. Thanks
  6. #4
  7. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    861
    sticking with your code, maybe something like this will work:

    Code:
    my %files = get_CSV_Files($folderName);
    foreach my $keys (sort{$files{$a} <=> $files{$b}} keys %files) {
    	print "$keys\t", scalar localtime($files{$keys}), "\n";
    }
    
    sub get_CSV_Files {
       my $dir = shift;
       my $dh = DirHandle->new($dir); #die "can't opendir $dir: $!";
       return  map {$_ => (stat($_))[9]}
               map  { "$dir$_" }
               grep { m/.txt/i }
               $dh->read();
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    46
    Rep Power
    9
    Originally Posted by KevinADC
    sticking with your code, maybe something like this will work:

    Code:
    my %files = get_CSV_Files($folderName);
    foreach my $keys (sort{$files{$a} <=> $files{$b}} keys %files) {
    	print "$keys\t", scalar localtime($files{$keys}), "\n";
    }
    
    sub get_CSV_Files {
       my $dir = shift;
       my $dh = DirHandle->new($dir); #die "can't opendir $dir: $!";
       return  map {$_ => (stat($_))[9]}
               map  { "$dir$_" }
               grep { m/.txt/i }
               $dh->read();
    }
    Kevin,

    Excellent.

    Thanks
    Sath.
  10. #6
  11. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    861
    hehehe.... you will want to change the .txt back to .csv of course!

IMN logo majestic logo threadwatch logo seochat tools logo