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 1397 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
    • Kevin van Haaren
      0 Offline Kevin van Haaren Tue, Jun 23 2020 3:20 PM

      Why sort at all? Sorting involves a lot of comparisons over and over again to insert the elements correctly. If you just want a list of problematic files you can do:

      $RootFolder = Show-PWFolderBrowserDialog
      
      $Docs2Export = Get-PWDocumentsBySearch -FolderPath $RootFolder.FullPath -GetVersionsToo -PopulatePath
      
      foreach ($f in $Docs2Export) {
          if ($f.fullpath.length -ge 240) {
              write-host "$($f.fullpath.length): $($f.fullpath)"
          }
      }
      

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Reply
    • Kevin van Haaren
      0 Offline Kevin van Haaren Tue, Jun 23 2020 3:20 PM

      Why sort at all? Sorting involves a lot of comparisons over and over again to insert the elements correctly. If you just want a list of problematic files you can do:

      $RootFolder = Show-PWFolderBrowserDialog
      
      $Docs2Export = Get-PWDocumentsBySearch -FolderPath $RootFolder.FullPath -GetVersionsToo -PopulatePath
      
      foreach ($f in $Docs2Export) {
          if ($f.fullpath.length -ge 240) {
              write-host "$($f.fullpath.length): $($f.fullpath)"
          }
      }
      

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Children
    • Brian Flaherty
      0 Offline Brian Flaherty Tue, Jun 23 2020 3:53 PM in reply to Kevin van Haaren

      Or something like this will work as well.

      $MaxLength = 240
      $Docs2Export = Get-PWDocumentsBySearch -FolderPath 'PowerShell' -GetVersionsToo -PopulatePath | Where-Object { ($_.FullPath).Length -gt $MaxLength }

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

    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