Forum Discussion
Fred_Elmendorf
Dec 19, 2023Brass Contributor
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...
- 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
Fred_Elmendorf
Dec 20, 2023Brass Contributor
Hi 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
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-Bohren
Dec 20, 2023Steel Contributor
I've extended the Script.
$LocationFolders = Get-ChildItem -Path \\fileshare\Department\Device\Mfg -Directory
#Create empty Array
$Result = @()
#Loop through Folders
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
$FileObject = [PSCustomObject]@{
FullName = $File.FullName
Lenght = $File.Lenght
CreationTime = $File.CreationTime
LastWriteTime = $File.LastWriteTime
}
#Add FileObject to Array
$Result += $FileObject
}
}
#Export CSV
$Result | Export-CSV -Path C:\Temp\FileStat.csv -NoTypeInformation -Encoding UTF8
Kind Regards
Andres
- 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- Fred_ElmendorfDec 20, 2023Brass ContributorI had copied some code from a previous task and included an if statement that wasn't needed. I'm getting the output I need now.
Thank you so much for your help!
Fred- Andres-BohrenDec 20, 2023Steel ContributorPerfect. Thank's for the Feedback