Daily file count in addition to total files in folder.

Brass Contributor

This script is working well, and providing me the specified information, but in addition to the total file count in each folder, I need the file counts by day for each folder, within the date range. Any assistance greatly appreciated. Here's what I have so far:

 

#Use present date/time to create a unique output file name
$dirs = Get-ChildItem "\\dataserver\datafolders\PQM\$mMake" -Directory
$now = (Get-Date).ToString() -replace ':','.' -replace '/','.'
$csvLog = "\\Myserver\myfolders\Documents\PowerShellOutput\$mMake Latest Files $startDate - $endDate $trendOrevent $now.csv"
$csvLog

foreach ($dir in $dirs) {

$recentFile = $null
$folder = $dir.Name
$directory = $dir.FullName

$filesCount = (Get-ChildItem $directory -Filter "$trendOrevent*.pqd" -Recurse `
| Where-Object { $_.CreationTime -ge $sds-and $_.CreationTime -lt $eds }).Count
$recentFile = Get-ChildItem $directory -Filter "$trendOrevent*.pqd" -Recurse | Sort-Object LastWriteTime -Descending| Select-Object -First 1
$recentFileName = $recentFile.Name
$recentFileLength = $recentFile.Length
$recentFileWriteTime = $recentFile.LastWriteTime

# This if statement finds all the folders with trend or event files and counts the ones in the date range
if ($recentFile) {

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

# This else statement finds all the folders that don't have trend or event files
} else {
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value "No files in Folder"
$object | Add-Member -MemberType NoteProperty -Name "File Size" -Value $recentFileLength
$object | Add-Member -MemberType NoteProperty -Name "Date Time" -Value $recentFileWriteTime
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
}

4 Replies

Hi @Fred_Elmendorf 

 

This should help you

 

$Dir = "\\dataserver\datafolders\PQM\$mMake"

$start = (Get-Date).AddDays(-30)

$end = Get-Date

$Files = Get-ChildItem $dir | Where-Object { $_.CreationTime -ge $start -and $_.CreationTime -lt $End } | Group {$_.LastWriteTime.ToString("yyyy-MM-dd")} | Sort Name
$files

 

AndresBohren_0-1681391036690.png

Regards

Andres Bohren

 

 

Hi Andres,

As I was adding your code to mine, and reviewing the output from my file structure, I realized that my problem statement was not correct. For this particular set of folders, new files are added to either the ...\trend or ...\event folder, with the file name constructed to reflect the date of the data contained within the file. I actually have two problems. Number 1, I need to be able to count all the files where the date portion of the file name is the same. Number 2, I need to be able to count all the files where the date portion of the LastWriteTime is the same. Below is an example collection of files from one of the ...\event folders. In this example, 6 files were written on 4/13/2023, 8 fies were written on 4/12/2023, and 15 fies were written on 4/11/2023. However, all of the file names indicate that they contain data from 4/12/2023 (20230412). So, rather than count files in a folder as I had originally described, I actually need one count of files according to their LastWriteTime and another count of files according to the date part of the file name. Thanks again for any help you can provide.

@Fred_Elmendorf 

For the Files with the Same Modified Date (including hh:mm)

$Files = Get-ChildItem $directory | Where-Object { $_.CreationTime -ge $start -and $_.CreationTime -lt $End } | Group {$_.LastWriteTime.ToString("yyyy-MM-dd hh:mm")} | Sort Name


#Filename: event-YYYYMMDDT >First 16 Characters you want to Compare

$Filenames = Get-ChildItem $directory | Where-Object { $_.CreationTime -ge $start -and $_.CreationTime -lt $End } | Group {$_.Name.SubString(0,16)} | Sort Name

 

How does that work?

 

Regards

Andres