Forum Discussion
Extracting a date from a file name and counting the number of files with that date
Something like this?
PS C:\> $Directory = "C:\GIT_WorkingDir\PowerShellScripts\Techcommunity\Files"
PS C:\> $FileStat = Get-ChildItem $directory | Group {$_.Name.SubString(0,6)} | Sort-Object Name | Select Name, Count
PS C:\> $FileStat
Name Count
---- -----
230216 2
230217 3
230218 1
Regards
Andres
- Fred_ElmendorfApr 25, 2023Copper Contributor
Greetings Andres,
As I attempted to incorporate your solution, I realized that my description of the problem was incomplete. I need to subset the list of file names according to a string contained in some of the files. The subsetting code is working, but I'm not getting your solution incorporated correctly to get the count. The code below produces a list of files that contain the specified string, but my results include all file names, not just a count of those who's substring dates are the same. See my code and present output below. I need to be able to apply the grouping and counting after the subsetting.
My subsetting code:
Get-ChildItem -Path "\\MyDirectory" -Filter "*.inf" -Recurse |
ForEach-Object {
$File = $_;
if ((Get-Content -Path ($_.FullName) -Raw) -match "$tStr") {
[PSCustomObject] @{
Created = $file.CreationTime
Filename = $File.FullName;
}
}
} |
Export-Csv -NoTypeInformation -Path "$csvlog"Present Output:
"Created","Filename"
"12/21/2022 1:57:22 AM","\MyDirectory\\221221,005519621,-6t,R79,F2384.inf"
"12/21/2022 3:31:19 AM","\MyDirectory\\221221,022923433,-6t,R79,F2385.inf"
"12/21/2022 8:09:30 AM","\MyDirectory\\221221,070653869,-6t,R79,F2386.inf"
"12/21/2022 1:24:20 PM","\MyDirectory\\221221,122230539,-6t,R79,F2390.inf"
"12/22/2022 4:32:25 AM","\MyDirectory\\221222,032959801,-6t,R79,F2391.inf"
"12/22/2022 5:40:25 AM","\MyDirectory\\221222,043833085,-6t,R79,F2392.inf"
"12/22/2022 11:34:32 PM","\MyDirectory\\221222,223339271,-6t,R79,F2393.inf"
"12/23/2022 1:06:39 AM","\MyDirectory\\221223,000458540,-6t,R79,F2394.inf"
"12/23/2022 2:10:45 AM","\MyDirectory\\221223,010925319,-6t,R79,F2396.inf"
"12/23/2022 4:14:32 AM","\MyDirectory\\221223,031330457,-6t,R79,F2397.inf"
"12/24/2022 2:34:47 PM","\MyDirectory\\221224,133325993,-6t,R79,F2402.inf"- Andres-BohrenApr 25, 2023Steel ContributorPS C:\> $Directory = "C:\GIT_WorkingDir\PowerShellScripts\Techcommunity\Files"
PS C:\> $FileStat = Get-ChildItem $directory -Filter "*.txt" | Group {$_.Name.SubString(0,6)} | Sort-Object Name | Select Name, Count
PS C:\> $filestat - Fred_ElmendorfApr 25, 2023Copper Contributor
This reply was not needed.