read files by descending

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    read files by descending

    In C:/perl I have
    a.txt
    b.txt
    c.txt
    d.txt


    Code:
    use File::Find;
    $filedir='C:/perl';
    
    find (sub { 
         local @ARGV = $_; 
    	
          print Dumper $_;
    
    }, $filedir);


    $VAR1 = 'a.txt';
    $VAR1 = 'b.txt';
    $VAR1 = 'c.txt';
    $VAR1 = 'd.txt';


    But i would like my file to be read in descending.
    $VAR1 = 'd.txt';
    $VAR1 = 'c.txt';
    $VAR1 = 'b.txt';
    $VAR1 = 'a.txt';


    How to do it? is it related to the setting of operating system?
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    You will need to use the sort function.

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Your question is quite vague, but if the filenames are really simple alpha strings then using sort will work. But it looks like your code is reading in one file at a time from the hard disk so there would be nothing to sort. You have to create a list before you can sort it.

      Comment

      • MyMarlboro
        New Member
        • Mar 2008
        • 71

        #4
        Thanks.
        Actually I wish to process the last file (which is d.txt) and then follow by C.txt and so on... where currently i force to process the files ascendingly.
        Is it the only way to list out all the file name and sort it descendingly and then process the file accordingly?
        Please guide.
        Thx.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Try using the opendir to open C:/perl, then build an array of all the files using readdir. Then you can sort the array and open the files in reverse order. It might look something like this:

          [CODE=perl]opendir my $dh, 'C:/perl';
          my @filenames;
          while (readdir $dh) {
          push @filenames, $_;
          }
          @filenames = sort {$b cmp $a} @filenames;
          for my $filename (@filenames) {
          open my $fh, '<', $filename;
          # Proceed with processing the file.
          }[/CODE]

          Comment

          • MyMarlboro
            New Member
            • Mar 2008
            • 71

            #6
            Thanks for your inputs.
            I would like to know is it the only way to do so?
            Can we change the file processing process (Not to put into array and do the sort) from ascending to descending or maybe based on the modified date of the files.
            Thanks.

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by MyMarlboro
              Thanks for your inputs.
              I would like to know is it the only way to do so?
              Can we change the file processing process (Not to put into array and do the sort) from ascending to descending or maybe based on the modified date of the files.
              Thanks.
              No. Files stored on disk are in no particular order. If you want to process files in a certain order (by name or date or whatever) you have to sort them first somehow. You can sort them with perl or with the operating system.

              Comment

              • MyMarlboro
                New Member
                • Mar 2008
                • 71

                #8
                Thank, especially always help me. Thanks.
                Noted. I have some question,
                1. file sort it by no particular order, but how come when I keep running the code above, it keep sort ascendingly for me?
                2. how it can be sorted by operating system?
                Thank you

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by MyMarlboro
                  Thank, especially always help me. Thanks.
                  Noted. I have some question,
                  1. file sort it by no particular order, but how come when I keep running the code above, it keep sort ascendingly for me?
                  2. how it can be sorted by operating system?
                  Thank you
                  There comes a time when you have to tell somone this:

                  Read the Manual

                  All operating sytems come with help files. Read the help files (or the manual) for your operating system and see whatever options there are for sorting files. Personally I think you will be better off just sticking with perl to sort the files if perl is also going to be processing the file.

                  Comment

                  • MyMarlboro
                    New Member
                    • Mar 2008
                    • 71

                    #10
                    Noted. I will try to find out.
                    Thank you.

                    Comment

                    Working...