header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Folder search with specific name format
Last Post 14 Aug 2012 07:49 PM by RiffyRiot. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
fat-chunkUser is Offline
New Member
New Member
Posts:12
Avatar

--
14 Aug 2012 02:01 AM
    Hi,

    I have a set of folders and I need to store the path of a particular one in a variable $folder.
    The set of folders are all children of a parent folder we'll call "parent". There are a bunch of folders with numeric names and one with a non numeric name.
    Here is an example of what is in the "parent" folder:

    3.0.2.3
    3.0.4.5
    4.0.1.3
    4.0.1.4
    4.0.1.6
    4.0.2.1
    4.0.2.2
    LOGS
    BUILD

    I want a powershell command that will store the path to the latest version folder (in this case "C:\parent\4.0.2.2") in my $folder variable.  I think the best approach would be to filter the non numeric folders with a regex (something like "/d./d./d./d")
    and then use the pipeline and gci along with a command that would take the item at the bottom of the list.

    However I do not have much experience with the Powershell pipeline and was wondering if any talented Powershell users could help me with this problem.

    Thanks!
    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    14 Aug 2012 03:03 AM
    Try this...

    
    $LatestVersion=(get-childitem 'c:\temp' | where-object {$_.PSIsContainer -and $_.Name -match '^\d'} | Sort-Object Name | select-object -last 1).Name
    $LatestVersion
    
    4.0.2.2
    


    In English:

    (Get all objects in c:\temp | where object is a folder the and object name starts^ with a digit\d | sort all the objects by name(default ascending) | select the last object).Get the objects Name

    I hope the explanation helps... it took longer to write than the code ;-)
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    14 Aug 2012 03:14 AM
    Oops, you wanted the full path? Use .FullName at the end instead of .Name
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    fat-chunkUser is Offline
    New Member
    New Member
    Posts:12
    Avatar

    --
    14 Aug 2012 06:28 AM
    This works a charm!

    Thank you very much!
    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    14 Aug 2012 07:49 PM
    Great! Could you mark the thread as resolved?
    Thanks.
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer