Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise PowerShell Extensions Forum sorting an array to find the longest folder path
    • Sign In

    • State Verified Answer
    • Replies 6 replies
    • Subscribers 66 subscribers
    • Views 1370 views
    • Users 0 members are here

    sorting an array to find the longest folder path

    Stephen Herrick
    Offline Stephen Herrick over 3 years ago

    PS Community,

    I am doing an audit on a project to get the length of the folder structure, so as to ensure it can be exported out,

    This is how I am populating the folder structure

    $RootFolder = Show-PWFolderBrowserDialog

    $Docs2Export = Get-PWDocumentsBySearch -FolderPath $RootFolder.FullPath -GetVersionsToo -PopulatePath

    I want to sort Ascending to get the longest folder, but there is no option, even if I use -Descending it isn't sorting it right either

    $Docs2Export.FullPath | Sort-Object -Descending { $_.FullPath.Length} | Select -Firt 1

    What am I doing wrong,

    TIA

    Stephen

    • Sign in to reply
    • Cancel

    Top Replies

    • Blandry
      Offline Blandry Tue, Jun 23 2020 10:48 AM +1 verified
      There might be a better way, but i would handle this with an array. $RootFolder = Show-PWFolderBrowserDialog $Docs = Get-PWDocumentsBySearch -FolderPath $RootFolder.FullPath -GetVersionsToo -PopulatePath…
    Parents
    • Blandry
      +1 Offline Blandry Tue, Jun 23 2020 10:48 AM

      There might be a better way, but i would handle this with an array. 

      $RootFolder = Show-PWFolderBrowserDialog
      $Docs = Get-PWDocumentsBySearch -FolderPath $RootFolder.FullPath -GetVersionsToo -PopulatePath

      $Array = @()

      ForEach($Doc in $Docs)
      {
      $Path = $Doc.FullPath
      $Array += $Path


      }

      $Array|Sort-Object {$_.Length} -Descending

      Answer Verified By: Stephen Herrick 

      • Cancel
      • Vote Up +1 Vote Down
      • Sign in to reply
      • Reject Answer
      • Cancel
    • Blandry
      0 Offline Blandry Tue, Jun 23 2020 10:53 AM in reply to Blandry

      The above gives you an array with just paths in it. 

      This is the other way

      $RootFolder = Show-PWFolderBrowserDialog

      $Docs = Get-PWDocumentsBySearch -FolderPath $RootFolder.FullPath -GetVersionsToo -PopulatePath

      $Docs.FullPath |Sort-Object {$_.Length} -Descending

      Same results but this array has all the other stuff in it too. You would need to do a little more filtering as this would give you the longest character count in a path. not the deepest folder structure. But i think i remember a new cmdlet being added that gets a folder structure and returns just that but i cant seem to find it. 

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Reply
    • Blandry
      0 Offline Blandry Tue, Jun 23 2020 10:53 AM in reply to Blandry

      The above gives you an array with just paths in it. 

      This is the other way

      $RootFolder = Show-PWFolderBrowserDialog

      $Docs = Get-PWDocumentsBySearch -FolderPath $RootFolder.FullPath -GetVersionsToo -PopulatePath

      $Docs.FullPath |Sort-Object {$_.Length} -Descending

      Same results but this array has all the other stuff in it too. You would need to do a little more filtering as this would give you the longest character count in a path. not the deepest folder structure. But i think i remember a new cmdlet being added that gets a folder structure and returns just that but i cant seem to find it. 

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Children
    No Data

    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2023 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies