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.
This topic is 3 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
mm_sho
Posts: 2
Last visit: Sun Jul 05, 2020 1:27 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.     ForEach-Object {
  3.         $NewestItem = Get-ChildItem -Path $_.FullName -File -Recurse | Sort-Object -Property LastWriteTime | Select-Object -Last 1
  4.         [PSCustomObject]@{
  5.             Folder = $_.FullName
  6.             NewestItem = $NewestItem.FullName
  7.             NewestItemDate = $NewestItem.LastWriteTime
  8.         }
  9.     }
  10. $Result | Export-Csv -Path .\output.csv -NoTypeInformation
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Sun Jul 05, 2020 1:27 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.
This topic is 3 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked