Forum Discussion

Fred_Elmendorf's avatar
Fred_Elmendorf
Brass Contributor
Feb 14, 2023
Solved

Count of files in specific sub-folders

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 t...
  • Leavii's avatar
    Leavii
    Feb 15, 2023

    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
    
        }
    
    }

     

     

     

     

     

     

     

Resources