in reply to Re: Get newest folders inside directory
in thread Get newest folders inside directory

Thanks for your reply. Where would I specify the root folder I want to check?
  • Comment on Re^2: Get newest folders inside directory

Replies are listed 'Best First'.
Re^3: Get newest folders inside directory
by BrowserUk (Patriarch) on Nov 09, 2010 at 23:47 UTC

    In the quotes before the '*'. If you're on win, you might want to include a drive specifier in there also: glob 'c:/*'

    @newest = (sort{ -M $a <=> -M $b } grep -d, glob '/*' )[0..19];;

    Also look at -A & -C as applicable on your OS.

      Hi,
      This works great.
      Can anybody explain how this works internally? i mean which executes first and which next, left to right etc. what is the input to sort? I am new to Perl.

      Thanks

        Read the comments in number order:

        ## which are finally assigned to the array ## 4 # +# my @newest = ( ## This runs next, and sorts the directories ## 2 +## ## from most recent to least recent sort{ -M $a <=> -M $b } ## this runs first, generates a list of files/dirs in root ## 1 +## ## which it then reduces to just a list of directories grep -d, glob '/*' )[0..19]; ## then this "list slice" selects the 20 most recent ## 3 # +#

        It is equivalent to:

        my @allFilesnDirs = glob '/*'; my @allDirs = grep -d, @allFilesnDirs; my @allDirsSortByDateTime = sort{ -M $a <=> -M $b } @allDirs; my @newest20 = @allDirsSortedByDateTime[ 0..19 ];

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks a lot, that works great! :-)