Last modified File starting 2 folders deep, cycling through all subfolders

Welcome Forums General PowerShell Q&A Last modified File starting 2 folders deep, cycling through all subfolders

Viewing 6 reply threads
  • Author
    Posts
    • #236551
      Participant
      Topics: 3
      Replies: 15
      Points: 50
      Rank: Member

      How do I combine these two scripts to pull the info from the first one (latest modified file with date/time), but start two levels down and loop through all subfolders like in the second one.  When I insert the first one into the second at “Get-Item”, it errors out.

      Get-ChildItem -Recurse  | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize 
      $StartLevel = 2 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
      $Depth = 15      # How many levels deep to scan
      $Path = "."     # starting path
      
      For ($i=$StartLevel; $i -le $Depth; $i++) {
          $Levels = "\*" * $i
          (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
          Select FullName
      }
    • #236590
      Participant
      Topics: 1
      Replies: 4
      Points: 25
      Rank: Member

      try this

       

      $StartLevel = 2 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
      $Depth = 15 # How many levels deep to scan
      $Path = "." # starting path
      
      $folders = For ($i=$StartLevel; $i -le $Depth; $i++) {
      $Levels = "\*" * $i
      (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
      Select FullName
      }
      
      $Folders.fullname | %{ Get-ChildItem -path $_ -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize }
    • #236596
      Participant
      Topics: 3
      Replies: 15
      Points: 50
      Rank: Member

      WOW!  I think that’s it!!

      It runs well, except I am getting this error and I think it’s happening when there aren’t subfolders to 15 maybe?

      Get-Item : Cannot bind argument to parameter ‘Path’ because it is null.
      At line:7 char:44
      + (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsConta …
      + ~~~~~~~~
      + CategoryInfo : InvalidData: (:) [Get-Item], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemComm
      and

    • #236605
      Participant
      Topics: 3
      Replies: 15
      Points: 50
      Rank: Member

      I’m thrilled with what you have done so far!!

      How do I fix the Null error?

      How do I output the results to a file?

       

      And how do I mark your answer as awesome?

    • #236665
      Participant
      Topics: 3
      Replies: 15
      Points: 50
      Rank: Member

      I got it to output to file.  How do I fix the error when the subfolder doesn’t exist down to the 15 level?

      Get-Item : Cannot bind argument to parameter ‘Path’ because it is null.
      At line:7 char:44
      + (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsConta …
      + ~~~~~~~~
      + CategoryInfo : InvalidData: (:) [Get-Item], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemComm
      and

    • #236680
      Participant
      Topics: 3
      Replies: 15
      Points: 50
      Rank: Member

      or suppress it

    • #236725
      Participant
      Topics: 3
      Replies: 417
      Points: 1,462
      Helping Hand
      Rank: Community Hero

      I think if you just check if it is something like this

      $Folders.fullname | ?{$_} | %{ Get-ChildItem -path $_ -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize }

      Or maybe if you don’t want to see them just ignore them.

      $Folders.fullname | %{ Get-ChildItem -path $_ -Recurse -erroraction silentlycontinue | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize }
Viewing 6 reply threads
  • You must be logged in to reply to this topic.