search text files for multiple keywords

  • How can I modify this script to do a couple more features.

    1) process only the most current file from the folder.

    2) if I find 4 occurrences of the word FULL in that most recent file .. then issue an email

    many thanks to the people in this forum.

    # Define the path to the directory containing text files and the keywords to search for
    $directoryPath = "\\xsxgg1\pub\nepr\performance\aimonitor\mfgprod\"
    $keywords = @("FULL")

    # Get all text files in the directory
    $textFiles = Get-ChildItem -Path $directoryPath -Filter *.*

    # Search for the keywords in each text file
    foreach ($file in $textFiles) {
    $results = Select-String -Path $file.FullName -Pattern $keywords
    $results | ForEach-Object {
    Write-Output "Found '$($_.Matches.Value)' in file '$($file.Name)' at line $($_.LineNumber): $($_.Line)"
    }
    }

     

  • Okay I have the getting most current file now.

    $textFiles = Get-ChildItem -Path $directoryPath |?{-not $_.PsIsContainer} | Sort CreationTime | Select -Last 1

    How do I count found Keyword now if Equal to or GT 4 issue email

  • okay now I'm close on counting keyword but I have something wrong I know .value isn't correct..

    $keywords = @("EMPTY")

    $textFiles = Get-ChildItem -Path $directoryPath |?{-not $_.PsIsContainer} | Sort CreationTime | Select -Last 1

    # Search for the keywords in each text file

    foreach ($file in $textFiles) {

    $results = Select-String -Path $file.FullName -Pattern $keywords

    $results | ForEach-Object {

    # Sum the numbers found in the line

    foreach ($number in $results) {

    $totalSum += [int]$number.Value

    }

    $totalSum   ----????????????

    }

    }

  • Any suggestions or help..

    Many thanks.

  • Bruin wrote:

    Any suggestions or help..

    Many thanks.

    I don't know 5hit from Shinola in Powershell and so I tried something different... here's the link to the thread I worked through...

    https://www.perplexity.ai/search/using-powershell-how-to-search-hGZTTLw2Rmi4v81v3IwqEA#2

    I'm not testing it for you though.  You have to have some of the fun.  Make sure that you read both questions leading up to that final 3rd question. You might also want to read some of the links it provides.  And, always remember, it's "just a consensus" engine when it comes to such things so you must ALWAYS test things including any and all "Edge Cases".

    p.s. Saying "AI it" just doesn't have the same ring as "Google it" does, though.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden wrote:

    Bruin wrote:

    Any suggestions or help..

    Many thanks.

    I don't know 5hit from Shinola in Powershell and so I tried something different... here's the link to the thread I worked through...

    https://www.perplexity.ai/search/using-powershell-how-to-search-hGZTTLw2Rmi4v81v3IwqEA#2

    I'm not testing it for you though.  You have to have some of the fun.  Make sure that you read both questions leading up to that final 3rd question. You might also want to read some of the links it provides.  And, always remember, it's "just a consensus" engine when it comes to such things so you must ALWAYS test things including any and all "Edge Cases".

    p.s. Saying "AI it" just doesn't have the same ring as "Google it" does, though.

    p.p.s. I am NOT responsible for any answers that AI may or may not provide... Period!

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply