SOLVED

Count of files in specific sub-folders

Brass Contributor

I'm a PowerShell novice that has benefited greatly from examples and comments in this community. I'm presently using this code to produce a list that includes folder name, file name, and last write time of the most recent file in each folder at Level6, in this example, where the file name has "event" as part of the name. It is producing the desired results:

"Firstfolder","event123file.pqd","2/14/2023 10:58:10 AM"

"Nextfolder","eventabcfile.pqd","2/14/2023 8:53:36 PM"

 

I need to add code to include the total file count for each folder in the output list. Here's the code I'm using now.

 

$path="\\Level1\Level2\Level3\Level4\Level5"
$NoOfDirs=Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $True}
ForEach($dir in $NoOfDirs )
{

Get-ChildItem "$path\$($dir.name)" -Recurse |
Where-Object {($_.name -Like "event*.pqd") -and ($_.PSIsContainer -eq $False) } |
Select-Object @{l='Folder';e={$dir.Name}},Name,LastWriteTime |
Sort-Object -pro LastWriteTime -Descending |
#Specify -First N for number of most recent days
Select -First 1 | export-csv "\\Level1\Level2\MyFolders\MySubfolders\Documents\PowerShellOutput\LatestFiles.csv" -append -NoTypeInformation
}

 

Since I have very little experience with PowerShell, detailed feedback will be greatly appreciated!

Thanks in advance!

 

5 Replies

@Fred_Elmendorf 

 

With all them pipes I can't see how you could get file counts and export in this manner.  This is what I typically do for exports and working with directories.  If it isn't exactly right should get you on your way I'd think.

 

 

 

$dirs = Get-ChildItem "\\Level1\Level2\Level3\Level4\Level5" -Directory
$csvLog = "C:\log\path\file.csv"


foreach ($dir in $dirs) {
    
    $folder = $dir.Name
    $directory = $dir.FullName
    $filesCount = (Get-ChildItem $directory -Filter "event*.pqd" -Recurse).Count
    $recentFile = Get-ChildItem $directory -Filter "event*.pqd" -Recurse | Sort-Object LastWriteTime -Descending| Select-Object -First 1
    $recentFileName = $recentFile.Name
    $recentFileWriteTime = $recentFile.LastWriteTime

    $object = New-Object -TypeName psobject
    $object | Add-Member -MemberType NoteProperty -Name "Folder" -Value $folder
    $object | Add-Member -MemberType NoteProperty -Name "File" -Value $recentFileName
    $object | Add-Member -MemberType NoteProperty -Name "LastWriteTime" -Value $recentFileWriteTime
    $object | Add-Member -MemberType NoteProperty -Name "Count" -Value $filesCount
    $object | Export-Csv $csvLog -Encoding ASCII -Append

}

 

 

 

I appreciate your excellent response. I accomplishes exactly what I described, but I left out one detail. There are folders that do not contain "event*.pqd" files. How do I eliminate them from the output?

Thanks in advance!
best response confirmed by Fred_Elmendorf (Brass Contributor)
Solution

@Fred_Elmendorf 

 

The simplest solution to me would be to check if it found a recent .pqd file then export.  I don't claim this as the best or cleanest solution, but should work.

 

 

 

 

 

 

 

$ErrorActionPreference = "SilentlyContinue"
$dirs = Get-ChildItem "\\Level1\Level2\Level3\Level4\Level5" -Directory
$csvLog = "C:\log\path\file.csv"

foreach ($dir in $dirs) {
    
    $recentFile = $null
    $folder = $dir.Name
    $directory = $dir.FullName    
    $recentFile = Get-ChildItem $directory -Filter "event*.pqd" -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 1

    if ($recentFile) {

        $filesCount = (Get-ChildItem $directory -Filter "event*.pqd" -Recurse).Count
        $recentFileName = $recentFile.Name
        $recentFileWriteTime = $recentFile.LastWriteTime

        $object = New-Object -TypeName psobject
        $object | Add-Member -MemberType NoteProperty -Name "Folder" -Value $folder
        $object | Add-Member -MemberType NoteProperty -Name "File" -Value $recentFileName
        $object | Add-Member -MemberType NoteProperty -Name "LastWriteTime" -Value $recentFileWriteTime
        $object | Add-Member -MemberType NoteProperty -Name "Count" -Value $filesCount
        $object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation

    }

}

 

 

 

 

 

 

 

Yes! That cleans up the results.
Thank you!

@Fred_Elmendorf 

 

Glad I could help.

1 best response

Accepted Solutions
best response confirmed by Fred_Elmendorf (Brass Contributor)
Solution

@Fred_Elmendorf 

 

The simplest solution to me would be to check if it found a recent .pqd file then export.  I don't claim this as the best or cleanest solution, but should work.

 

 

 

 

 

 

 

$ErrorActionPreference = "SilentlyContinue"
$dirs = Get-ChildItem "\\Level1\Level2\Level3\Level4\Level5" -Directory
$csvLog = "C:\log\path\file.csv"

foreach ($dir in $dirs) {
    
    $recentFile = $null
    $folder = $dir.Name
    $directory = $dir.FullName    
    $recentFile = Get-ChildItem $directory -Filter "event*.pqd" -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 1

    if ($recentFile) {

        $filesCount = (Get-ChildItem $directory -Filter "event*.pqd" -Recurse).Count
        $recentFileName = $recentFile.Name
        $recentFileWriteTime = $recentFile.LastWriteTime

        $object = New-Object -TypeName psobject
        $object | Add-Member -MemberType NoteProperty -Name "Folder" -Value $folder
        $object | Add-Member -MemberType NoteProperty -Name "File" -Value $recentFileName
        $object | Add-Member -MemberType NoteProperty -Name "LastWriteTime" -Value $recentFileWriteTime
        $object | Add-Member -MemberType NoteProperty -Name "Count" -Value $filesCount
        $object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation

    }

}

 

 

 

 

 

 

 

View solution in original post