Windows Vista Forums

Sort via creation time

  1. #1


    Mark Line Guest

    Sort via creation time

    Hi,
    I was wondering if anyone could help me,
    I’m trying to get a folder name into a variable so I will be able to copy
    the folder to another location.

    I want the newest folder in a certain directory, the following commands
    returns the correct information:

    $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select -last 1 |
    select name

    However $newestDir now contains “@{Name=FolderName}”

    I only want the name of the folder so I can append it to a path for copying
    Any help would be fantastic?
    Thanks
    Mark


      My System SpecsSystem Spec

  2.   


  3. #2


    Jacob Saaby Nielsen Guest

    Re: Sort via creation time

    Hello Mark,

    I modified your line a bit (sort is sort-object, and the current object in
    the pipeline should be sorted on
    its property creationtime), and it works now

    PS C:\test> $newestDir = ls | ?{$_.psiscontainer} | sort-object $_.creationtime
    | select -last 1 | select name
    PS C:\test> $newestdir.name
    2007-11-02-09

    Best Regards,
    Jacob Saaby Nielsen
    mailto:jacob.saaby@xxxxxx

    > Hi,
    > I was wondering if anyone could help me,
    > I’m trying to get a folder name into a variable so I will be able to
    > copy
    > the folder to another location.
    > I want the newest folder in a certain directory, the following
    > commands returns the correct information:
    >
    > $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
    > -last 1 | select name
    >
    > However $newestDir now contains “@{Name=FolderName}”
    >
    > I only want the name of the folder so I can append it to a path for
    > copying
    > Any help would be fantastic?
    > Thanks
    > Mark


      My System SpecsSystem Spec

  4. #3


    Mark Line Guest

    Re: Sort via creation time

    Hi Jacob,

    That works like a treat! thank you very much for your help :-)

    Mark

    "Jacob Saaby Nielsen" wrote:

    > Hello Mark,
    >
    > I modified your line a bit (sort is sort-object, and the current object in
    > the pipeline should be sorted on
    > its property creationtime), and it works now
    >
    > PS C:\test> $newestDir = ls | ?{$_.psiscontainer} | sort-object $_.creationtime
    > | select -last 1 | select name
    > PS C:\test> $newestdir.name
    > 2007-11-02-09
    >
    > Best Regards,
    > Jacob Saaby Nielsen
    > mailto:jacob.saaby@xxxxxx
    >

    > > Hi,
    > > I was wondering if anyone could help me,
    > > I’m trying to get a folder name into a variable so I will be able to
    > > copy
    > > the folder to another location.
    > > I want the newest folder in a certain directory, the following
    > > commands returns the correct information:
    > >
    > > $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
    > > -last 1 | select name
    > >
    > > However $newestDir now contains “@{Name=FolderName}”
    > >
    > > I only want the name of the folder so I can append it to a path for
    > > copying
    > > Any help would be fantastic?
    > > Thanks
    > > Mark
    >
    >
    >

      My System SpecsSystem Spec

  5. #4


    Shay Levi Guest

    Re: Sort via creation time

    Hi mark

    You can get it in two ways

    get-childitem | where {$_.psiscontainer} | sort creationtime -descending
    | select name -first 1

    Or

    get-childitem | where {$_.psiscontainer} | sort creationtime | select name
    -last 1



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com
    Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



    > Hi,
    > I was wondering if anyone could help me,
    > Im trying to get a folder name into a variable so I will be able to
    > copy
    > the folder to another location.
    > I want the newest folder in a certain directory, the following
    > commands returns the correct information:
    >
    > $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
    > -last 1 | select name
    >
    > However $newestDir now contains @{Name=FolderName}
    >
    > I only want the name of the folder so I can append it to a path for
    > copying
    > Any help would be fantastic?
    > Thanks
    > Mark


      My System SpecsSystem Spec

  6. #5


    Jacob Saaby Nielsen Guest

    Re: Sort via creation time

    Goes to show how flexible PS really is. I know I'm still pretty impressed
    with it ;o)

    Best Regards,
    Jacob Saaby Nielsen
    mailto:jacob.saaby@xxxxxx

    > Hi mark
    >
    > You can get it in two ways
    >
    > get-childitem | where {$_.psiscontainer} | sort creationtime
    > -descending | select name -first 1
    >
    > Or
    >
    > get-childitem | where {$_.psiscontainer} | sort creationtime | select
    > name -last 1
    >
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com
    > Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic

    >> Hi,
    >> I was wondering if anyone could help me,
    >> I’m trying to get a folder name into a variable so I will be able to
    >> copy
    >> the folder to another location.
    >> I want the newest folder in a certain directory, the following
    >> commands returns the correct information:
    >> $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select
    >> -last 1 | select name
    >>
    >> However $newestDir now contains “@{Name=FolderName}”
    >>
    >> I only want the name of the folder so I can append it to a path for
    >> copying
    >> Any help would be fantastic?
    >> Thanks
    >> Mark


      My System SpecsSystem Spec

  7. #6


    Keith Hill [MVP] Guest

    Re: Sort via creation time

    "Mark Line" <Mark Line@xxxxxx> wrote in message
    news:07CECB51-D69C-4C53-8DA1-60EDDBAE3E07@xxxxxx

    > Hi,
    > I was wondering if anyone could help me,
    > I’m trying to get a folder name into a variable so I will be able to copy
    > the folder to another location.
    >
    > I want the newest folder in a certain directory, the following commands
    > returns the correct information:
    >
    > $newestDir = ls | ?{$_.psiscontainer} | sort creationtime | select -last 1
    > |
    > select name
    >
    > However $newestDir now contains “@{Name=FolderName}”
    >
    > I only want the name of the folder so I can append it to a path for
    > copying
    > Any help would be fantastic?
    > Thanks
    > Mark
    >
    Keep in mind that Select <propertyName> is like a SQL select I.e. it
    projects the selected columns (properties in PowerShell) onto a new table
    (PSCustomObject in PowerShell). That custom object has properties named the
    same as the properties "selected" into that projection (like the column
    names in SQL). If you really only want the path (unadorned) then try this:

    $newestDir = ls | ?{$_.psiscontainer} | sort creationtime -desc |
    select -first 1 | foreach{$_.Name}

    --
    Keith


      My System SpecsSystem Spec


Sort via creation time
Similar Threads
Thread Forum
How to sort newsgroup messages by MESSAGE creation date (not THREAD) Live Mail
Contacts - Must wait for sort each time opened Vista mail
Checking dir creation time PowerShell
sort by file creation date Vista General
Trouble grouping System.IO.FileInfo objects by creation day, not time! PowerShell