Forum Discussion
I need the latest file in each level 6 subfolder in this file structure
- Dec 19, 2023
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
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
- Fred_ElmendorfDec 20, 2023Brass ContributorHi Andres,
Your code worked beautifully, first try. Thank you!
I need to get the output into a .csv file rather than the console, but I was trying to keep the problem as simple as possible to get the desired output. I'll work on that and may be able to pull from some other tasks I've already done. But if you have a solution for that, I'm sure your code will be more efficient than mine.
Thanks!
Fred- Andres-BohrenDec 20, 2023Steel Contributor
I've extended the Script.
$LocationFolders = Get-ChildItem -Path \\fileshare\Department\Device\Mfg -Directory#Create empty Array$Result = @()#Loop through FoldersForeach ($LocationFolder in $LocationFolders){Write-Host "Working on Folder: $($LocationFolder.FullName)" -ForegroundColor Green$TypeFolders = Get-ChildItem -Path $LocationFolder.FullName -DirectoryForeach ($TypeFolder in $TypeFolders){Write-Host "Working on Folder: $($TypeFolder.FullName)" -ForegroundColor Green$Files = Get-ChildItem -Path $TypeFolder.FullName -FileWrite-Host "Files: $($Files.Count)"$File = $Files | Sort-Object LastWriteTime -Descending | select -First 1#$File | fl FullName, Length, CreationTime, LastWriteTime$FileObject = [PSCustomObject]@{FullName = $File.FullNameLenght = $File.LenghtCreationTime = $File.CreationTimeLastWriteTime = $File.LastWriteTime}#Add FileObject to Array$Result += $FileObject}}#Export CSV$Result | Export-CSV -Path C:\Temp\FileStat.csv -NoTypeInformation -Encoding UTF8Kind RegardsAndres- Fred_ElmendorfDec 20, 2023Brass ContributorHi Andres,
Yes, that worked.
The only remaining issue is that some of the Type folders don't have any files, but I still need them to show up in my output with 0 file count.
Any suggestions?
Thanks,
Fred