Forum Discussion

Fred_Elmendorf's avatar
Fred_Elmendorf
Brass Contributor
Dec 19, 2023
Solved

I need the latest file in each level 6 subfolder in this file structure

\\fileshare\Department\Device\Mfg\Location 1\Type1\somefile.ext \\fileshare\Department\Device\Mfg\Location 1\Type2\somefile.ext \\fileshare\Department\Device\Mfg\Location 1\Type3\somefile.ext \\fi...
  • Andres-Bohren's avatar
    Dec 19, 2023

    Hi Fred_Elmendorf 

     

    Try this

     

    $LocationFolders = Get-ChildItem -Path \\fileshare\Department\Device\Mfg -Directory

    Foreach ($LocationFolder in $LocationFolders)
    {
    Write-Host "Working on Folder: $($LocationFolder.FullName)" -ForegroundColor Green
    $TypeFolders = Get-ChildItem -Path $LocationFolder.FullName -Directory
    Foreach ($TypeFolder in $TypeFolders)
    {
    Write-Host "Working on Folder: $($TypeFolder.FullName)" -ForegroundColor Green
    $Files = Get-ChildItem -Path $TypeFolder.FullName -File
    Write-Host "Files: $($Files.Count)"
    $File = $Files | Sort-Object LastWriteTime -Descending | select -First 1
    $File | fl FullName, Length, CreationTime, LastWriteTime
    }
    }

     

    Regards

    Andres