I have a task that I am trying to resolve and I thought I would have a go at using PowerShell.
From this tutorial I found out that I can read a text file and display it like this:
# C:\Users\Andrew> Get-Content -Path d:\TextToFind.txt
Then, based on another tutorial I tried to do a serach in text files for a phrase:
$Path = "D:\My Programs\2017\MeetSchedAssist\Meeting Schedule Assistant"
$Text = "ID_STR_THIS_VERSION"
$PathArray = @()
$Results = "D:\Results.txt"
# But I want to IGNORE "resource.h"
# But I want to filter for *.h AND *.cpp
Get-ChildItem $Path -Filter "*.cpp" | Where-Object { $_.Attributes -ne "Directory"}
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
$PathArray += $_.FullName
}
}
Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}
Doesn't work:
Specially, what I am wanting to do is this:
For each line of text in TextToFind.txt
Examine all CPP and H files in folder XXX - but ignore RESOURCE.H
If the file DOES NOT use this line of text
Append the line of text to a log file.
End If
End For
I know that the script written does not do this. But I am failing at the furst hurdle.
Update
Based on the comments and answer I have tried this:
# Read in the STRINGTABLE ID values I want to locate
$TextToFind = Get-Content -Path d:\TextToFind.txt
$Path = "D:\My Programs\2017\MeetSchedAssist\Meeting Schedule Assistant"
$Text = "ID_STR_THIS_VERSION"
$PathArray = @()
$Results = "D:\Results.txt"
# But I want to IGNORE "resource.h"
# But I want to filter for *.h AND *.cpp
# First you collect the files corresponding to your filters
$files = Get-ChildItem $Path -Filter "*.cpp" | Where-Object { $_.Attributes -ne "Directory"}
# Now iterate each of these text values
$TextToFind | ForEach-Object {
$Text = $_
Write-Host "Checking for: " $Text
# Then, you enumerate these files and search for your pattern
$InstancesFound = $FALSE
$files | ForEach-Object {
If ((Get-Content $_.FullName) | Select-String -Pattern $Text) {
$PathArray += $Text + " " + $_.FullName
$InstancesFound = $TRUE
}
}
if($InstancesFound -eq $FALSE) {
$PathArray += $Text + " No instance found in the source code!"
}
}
Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}
The only issue with the above is that it does not factor for ignoring resource.h and I can't seem to filter for .h and .cpp.

ForEach-Object {For what object? You do not pass anything toForeach-ObjectCMDlet. Just add the pipe|afterWhere-Objectexpression.