Forum Discussion
Fred_Elmendorf
Apr 20, 2023Copper Contributor
Extracting a date from a file name and counting the number of files with that date
I have a folder structure that contains thousands of files constructed with a date as part of the file name. I need to be able to reconstruct the date from the file name, count the number of files with that date string in the name. In the example below the first entry includes 230216, which represents February 16, 2023. Two files in this list have that date in their name, followed by three files on February 17, 2023. I need to be able to examine every file name in the folder and produce a CSV output file in this format:
Year, Month, Day, Count
2023,02,16,2
2023,02,17,3
2023,02,18,5
...
Any assistance greatly appreciated!
4 Replies
Sort By
- Andres-BohrenSteel Contributor
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:\> $FileStatName Count
---- -----
230216 2
230217 3
230218 1Regards
Andres
- Fred_ElmendorfCopper 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-BohrenSteel 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