File Counts - Multiple Directories - Looking for certain words

bnorrisatnortechsolutions
bnorrisatnortechsolutions used Ask the Experts™
on
I need a script that would search multiple directories for files that contain the words "performance" and then the word "experience" and return a count for each word.  

Then export the results to a CSV file that lists the results for directory 1 listing the performance files by name and then list the Experience files by name and then the total count for each type (i.e. Performance and Experience) and the same for directory 2 and so on......

The multiple directories would be listed in a DirList.txt file.


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Can the "Performance" or "experience" appear in the files more than once in a specific line?
The words "performance" and/or "experience" will only be in the file name once.

Commented:
In the file name or within the file?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

In the file name

Commented:
Hope you want something like this, as the output you requested is not very structured, but I tried to put it into a table:
$where = Get-Content C:\ee\dirlist.txt

$where | %{
    $actdir = $_
    "" | Select-Object @{n="Directory"; e={"$actdir"}}, Group, Files, PerformaceFiles, ExperienceFiles 
        
    dir $actdir | ? {!$_.psiscontainer -and $_.name -match "performance|experience"} | % {$p=$e=0} {
        $file = $_ 
        switch -regex ($_.name){
            performance {$p++; $file | Add-Member -MemberType noteproperty -Name Label -Value "Performance" -PassThru }
            experience {$e++; $file | Add-Member -MemberType noteproperty -Name Label -Value "Experience" -PassThru }
        }
    } | Group-Object -Property label | %{
        $group = $_
        "" | Select-Object Directory, @{n="Group"; e={$group.name}}, Files, PerformaceFiles, ExperienceFiles 
        Select-Object -InputObject $_ -ExpandProperty group  | Sort-Object -Property name |
            Select-Object @{n="Directory";e={""}},group,@{n="Files";e={$_.name}}, PerformaceFiles, ExperienceFiles 
    }
    New-Object -TypeName PSObject -Property @{
        Directory = $actdir
        PerformaceFiles = $p
        ExperienceFiles = $e
    } | Select-Object -Property Directory, group, PerformaceFiles, ExperienceFiles 
}  | Export-Csv c:\ee\pstat.csv -NoTypeInformation -UseCulture

Open in new window

That works for all files listed in the directories.  However, I only need the results to return the files that were created yesterday and today.

Commented:
You have not mentioned this before! I'll do it soon.
I apologize.  It was in my orginal post that I was going to post, but I removed it.
Commented:
Here you are:
$where = Get-Content C:\ee\dirlist.txt  
  
$where | %{  
    $actdir = $_  
    "" | Select-Object @{n="Directory"; e={"$actdir"}}, Group, Files, PerformaceFiles, ExperienceFiles   
          
    dir $actdir | ? {!$_.psiscontainer -and ((get-date) - $_.lastwritetime).days -le 1 -and $_.name -match "performance|experience"} | % {$p=$e=0} {  
        $file = $_   
        switch -regex ($_.name){  
            performance {$p++; $file | Add-Member -MemberType noteproperty -Name Label -Value "Performance" -PassThru }  
            experience {$e++; $file | Add-Member -MemberType noteproperty -Name Label -Value "Experience" -PassThru }  
        }  
    } | Group-Object -Property label | %{  
        $group = $_  
        "" | Select-Object Directory, @{n="Group"; e={$group.name}}, Files, PerformaceFiles, ExperienceFiles   
        Select-Object -InputObject $_ -ExpandProperty group  | Sort-Object -Property name |  
            Select-Object @{n="Directory";e={""}},group,@{n="Files";e={$_.name}}, PerformaceFiles, ExperienceFiles   
    }  
    New-Object -TypeName PSObject -Property @{  
        Directory = $actdir  
        PerformaceFiles = $p  
        ExperienceFiles = $e  
    } | Select-Object -Property Directory, group, PerformaceFiles, ExperienceFiles   
}  | Export-Csv c:\ee\pstat.csv -NoTypeInformation -UseCulture

Open in new window

I posted another question if you are interested.  If not, I understand that you have a life and other things to do besides write POSH scripts for me.  I do appreciate all of your respsonses though.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial