none
Search last date modified starting 2 folders down. RRS feed

  • Question

  • 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.

    $Result = Get-ChildItem -Path '.\Program 1\Projects 1-1000\*' -Directory |
        ForEach-Object {
            $NewestItem = Get-ChildItem -Path $_.FullName -File -Recurse | Sort-Object -Property LastWriteTime | Select-Object -Last 1
            [PSCustomObject]@{
                Folder = $_.FullName
                NewestItem = $NewestItem.FullName
                NewestItemDate = $NewestItem.LastWriteTime
            }
        }
    $Result | Export-Csv -Path .\output.csv -NoTypeInformation

    How do I modify this to start at the Project 1.1 level?

    .





    Friday, July 3, 2020 4:50 PM

All replies

  • Hi,

    I am not quite sure whether the project 1.1 you mentioned refers to the sub-folder 1.1.1 under the folder 1.1.

    If the answer is yes, in my test, the script can solve the problem for you.

    If not, please try to explain your question in detail, we will solve it for you as soon as possible.

    Best wishes,

    Young Yang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, July 6, 2020 2:01 AM
  • Hi,

    I am not quite sure whether the project 1.1 you mentioned refers to the sub-folder 1.1.1 under the folder 1.1.

    If the answer is yes, in my test, the script can solve the problem for you.

    If not, please try to explain your question in detail, we will solve it for you as soon as possible.

    Best wishes,

    Young Yang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Please take time to read and learn the forum requirements.  Do not post pictures of code or data.  It is useless to anyone.

    Please take some time to learn what a forum is and how technical forums work.


    \_(ツ)_/

    Monday, July 6, 2020 3:31 AM
    Moderator
  • When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

    https://powershell.org/forums/topic/limit-number-of-folders-script-runs-on-in-powershell/

    Thanks


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, July 6, 2020 6:34 AM
  • mmsho, 

    i tried to do some lab for your case. So in my lab i have next structure:

    D:\TMP\subfolder_test
    ---Project_1
    ------Project_1_1
    ...
    ------Project_1_1000
    ---Project_2
    ------Project_2_1
    ...
    ------Project_2_1000
    so code sample will be like this
    Get-ChildItem -Directory -Path d:\tmp\subfolder_test | foreach {
          $FirstLevelDir = $_.Fullname
          Get-ChildItem -Path $FirstLevelDir -Directory |
             select FullName, @{n='LastModifiedTime';e={$script:lastwriteTimeFile = Get-ChildItem $_.Fullname -Recurse | Sort LastWriteTime | Select -Last 1 ; $script:lastwriteTimeFile.LastWriteTime}}, @{n="LatestFileName";e={$script:lastwritetimefile.name}}
    } | Out-GridView
    result


    The opinion expressed by me is not an official position of Microsoft

    Tuesday, July 7, 2020 10:01 PM
  • Here is a little hint on how to do this the easy way.

    # get all folders at depth 2 (in this case 4 from root
    # numbers are based on the depth of the starting folder plus the relative depth minus 1
    Get-ChildItem c:\scripts -Directory -Depth 3|
        Where-Object{($_.Fullname -split '\\').count -eq 4}|
        Select-Object fullname

    The output can then process the folders in any y you want to process the subfolders.  This can be made into a function that can be called at-depth to further probe in different ways.

    We could also just get all folders and tag them with their depth then filter for the required depth.  It is basically the same as this but provides permanent extra information that can be useful with this type of problem.


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Wednesday, July 8, 2020 12:31 PM
    Tuesday, July 7, 2020 11:10 PM
    Moderator