Search LatWriteTime starting 2 folders down.

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
mm_sho
Posts: 2
Joined: Fri Jul 03, 2020 4:00 pm

Search LatWriteTime starting 2 folders down.

Post by mm_sho »

We have tons of archived projects. I'm looking to run through the folders, starting two folders deep and export the results to csv with the Folder path and the LastWriteTime.

Example of folder structure:

Projects 1-1000

***Project 1

******Project 1.1

******Project 1.2

There are thousands of projects which is why they are grouped 1-1000, 1001-2000, etc.

Ideally, the final list would look like this:

Folder Path\Project 1.1 July 21, 2000

Folder Path\Project 1.2 May 22, 2017

etc.

The date being the latest modified date of anything in the folder (Project 1.1) or any of the many sub-folders within Project 1.1 and so on for Project 1.2.

Our attorney will then choose how many years back we need to keep the data, so she may say Project 1.1 can be deleted because of how old it is.

This code works great, but I need to somehow add the -depth parameter to start at the Project 1.1 level.

How do I modify this to start at the Project 1.1 level?
  1. $Result = Get-ChildItem -Path '.\Program 1\Projects 1-1000\*' -Directory |
  2.  
  3.     ForEach-Object {
  4.  
  5.         $NewestItem = Get-ChildItem -Path $_.FullName -File -Recurse | Sort-Object -Property LastWriteTime | Select-Object -Last 1
  6.  
  7.         [PSCustomObject]@{
  8.  
  9.             Folder = $_.FullName
  10.  
  11.             NewestItem = $NewestItem.FullName
  12.  
  13.             NewestItemDate = $NewestItem.LastWriteTime
  14.  
  15.         }
  16.  
  17.     }
  18.  
  19. $Result | Export-Csv -Path .\output.csv -NoTypeInformation

jvierra
Posts: 14476
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Search LatWriteTime starting 2 folders down.

Post by jvierra »

The easiest way is to select the folders at level two then pass them to the code to obtain results. To do this just eliminate all folders that are a parent of the target folders.

mm_sho
Posts: 2
Joined: Fri Jul 03, 2020 4:00 pm

Re: Search LatWriteTime starting 2 folders down.

Post by mm_sho »

I'm not sure what you mean. I cannot move the folders and restructure the file system.

jvierra
Posts: 14476
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Search LatWriteTime starting 2 folders down.

Post by jvierra »

I didn't say anything about moving folders. You need to select the folders based on their location into a collection that you can use to perform your task.

Get all folders to the depth you want and eliminate all folders from the collection that are not at that depth.

Locked