I need to write a script that checks the first 3 to say 5 characters of a .csv file, then count those files and report the characters with the corresponding count.
I already have a few methods to do the simpler tasks. I've used a filter before to only select certain .csvs, using this:
Get-ChildItem C:\path -Recurse -Filter *.csv | Where {$_.Name -match 'NNN'}
I also use this to count the number of csvs in that corresponding location:
(Get-ChildItem C:\path -Recurse -Filter *.csv | Measure-Object).Count
How can I run a scan through a folder with say 3 random titles for the csv? Say they're RUNxxxxxxxx.csv, FUNxxxxxxx.csv, and TUNxxxxxxx.csv.
Edit: Let me explain more; basically, the example csvs I have above would be completely random, so it'd need to recognize those first 3 are different and only count those.
I'm not sure if a prompt inputting these would do any. The values are known, just different week to week (which is when this would be run;every week)
Thanks!
CodePudding user response:
You can accomplish that using the where-object filter combining three or statements which finds anything starting with the three letters you want and a wild card after.
Get-ChildItem C:\path -Recurse -Filter *.csv | Where {$_.Name -like "RUN*" -or $_.Name -like "Fun*" -or $_.Name -like "TUN*"}
You could also use the get-childitem -filter to accomplish this instead of searching for all .csv files and then use where-object.
Get-ChildItem C:\path -Recurse -Include "fUN*.csv","RUN*.csv","Tun*.csv"
CodePudding user response:
How about using a calculated property to add the desired part as additional property to be able to work with it?
Get-ChildItem -Recurse -Filter *.csv |
Where-Object -Property BaseName -Match -Value '^(r|t|f)un' |
Select-Object -Property @{Name = 'Part'; Expression={($_.BaseName).substring(0,3)}},* |
Group-Object -Property Part