'Refining powershell script

Am looking for some help to create a PowerShell script.

I have a folder where I have lots of files, I need only those file that has below two content inside it:

must have any matching string pattern as same as in file file1 (the content of file 1 is -IND 23042528525 or INDE 573626236 or DSE3523623 it can be more strings like this)

also have date inside the file in between 03152022 and 03312022 in the format mmddyyyy. file could be old so nothing to do with creation time. or if date patter is like effd//ukwn show the result like below Show path file name and which pattern in present in which file. I got the script from stackoverflow some1 helped me with that

 read the keywords from the file. Ensure special characters are escaped and join them with '|' (regex 'OR')
$keywords  = (Get-Content -Path 'C:\Users\username\Downloads\ISIN.txt' | ForEach-Object {[regex]::Escape($_)}) -join '|'
# create a regex to capture the date pattern (8 consecutive digits)
$dateRegex = [regex]'\b(\d{8})\b'  # \b means word boundary
# and a datetime variable to test if a found date is valid
$testDate   = Get-Date
# set two variables to the start and end date of your range (dates only, times set to 00:00:00)
$rangeStart = (Get-Date).AddDays(1).Date   # tomorrow
$rangeEnd   = [DateTime]::new($rangeStart.Year, $rangeStart.Month, 1).AddMonths(1).AddDays(-1)  # end of the month

# find all .txt files and loop through. Capture the output in variable $result
$result = Get-ChildItem -Path 'E:\data\PROD\server\InOut\Backup'-Filter '*.txt'-File -Recurse |
ForEach-Object {
    $content = Get-Content -Path $_.FullName -Raw
    # first check if any of the keywords can be found
    if ($content -match $keywords) {
        # now check if a valid date pattern 'MMddyyyy' can be found as well
        $dateFound = $false
        $match = $dateRegex.Match($content)
        while ($match.Success -and !$dateFound) {
            # we found a matching pattern. Test if this is a valid date and if so
            # set the $dateFound flag to $true and exit the while loop
            if ([datetime]::TryParseExact($match.Groups[1].Value, 
                                          'MMddyyyy',[CultureInfo]::InvariantCulture, 
                                          [System.Globalization.DateTimeStyles]::None, 
                                          [ref]$testDate)) {
                # check if the found date is in the set range
                # this tests INCLUDING the start and end dates
                $dateFound = ($testDate -ge $rangeStart -and $testDate -le $rangeEnd)
            }
            $match = $match.NextMatch()
        }
        # finally, if we also successfully found a date pattern, output the file
        if ($dateFound) { $_.FullName }
        elseif ($content -match '\bUNKNOWN\b') {
            # here you output again, because unknown was found instead of a valid date in range
            $_.FullName
        }
    }
}

# result is now either empty or a list of file fullnames
$result | set-content -Path 'C:\Users\username\Downloads\MatchedFiles.txt'

The above script is giving me the correct output but i want to know which file have which pattern inside referin the ISIN file. Suppoer ISIn have three pattern abc1 abc2 abc3 and gives the result like file1 file2 file4 file5 i want to result like abc2 in file 2 abc1 in file 1...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source