0

I cannot properly format my select-string so that I can use mulitple patterns. I am also trying to espcape the \'s in my pattern. Its using a directory path.

These are the patterns I need to search for:

Successfully FTPed file [\\BATCH1\TO\DENIAL.TXT 

Successfully FTPed file [\\BATCH1\TO\NEWORIGIN.TXT    

Successfully FTPed file [\\BATCH1\TO\ORIGIN.TXT    

Successfully FTPed file [\\BATCH1\TO\Ed.TXT

This code is working for just a single pattern search.

Get-ChildItem -Filter FTP_Outbound*.* -Path $rootFolder| Where-Object {$_.LastWriteTime -gt (get-date).AddDays(-.5)}| Select-String -pattern  ([regex]::Escape('Successfully FTPed file [\\BATCH1\CFGP\TO\DENIAL.TXT'))| % {
  Copy-Item -Path $_.Path -Destination 'c:\Temp' 
}

In the end, I would like it to both copy the file to the C:\Temp, and display the line it found in the file within the powershell window.

SAMPLE DATA:

[11:18:24 PM] Begin SFTP File \\BATCH1\TO\BRRef[20190724202940].txt To BRRef.txt
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\DENIAL.TXT[20190724194428]] to [\DENIAL.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\NEWORIGIN.TXT[20190724194428]] to [\NEWORIGIN.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\ORIGIN.TXT[20190724194428]] to [\ORIGIN.TXT]
[11:18:48 PM] Successfully FTPed file [\\BATCH1\TO\BRRef[20190724202940].txt] to [\BRRef.txt]
[11:18:48 PM] Update ProducedFile Set DeliveredFileName='BRRef.txt' Where CreateID=7710395
[11:18:48 PM] Successfully backed up file as :\\BATCH1\TO_BACKUP\BRRef[20190724202940]_20190724_231848.txt
FILE #2. [Ed.TXT[20190724194428]], begin FTP at [11:19:05 PM] 
Destination file name is: Ed.TXT[20190724194428]
After replaceing [], destination file name become: Ed.TXT
[11:19:05 PM] Begin SFTP File \\BATCH1\TO\Ed.TXT[20190724194428] To Ed.TXT
[11:19:07 PM] Successfully FTPed file [\\BATCH1\TO\Ed.TXT[20190724194428]] to [\Ed.TXT]
[11:19:07 PM] Update ProducedFile Set DeliveredFileName='Ed.TXT' Where CreateID=7710363
[11:19:07 PM] Successfully backed up file as :\\BATCH1\TO_BACKUP\Ed_20190724_231907.TXT[20190724194428]
6
  • 1
    [1] please REMOVE the extra powershell tags - 2,3,4 ... only ONE should be used. if you need to run on multiple versions, then list the lowest version. [2] please show some sample data AND the items you want to match AND the items you do not want to match. Commented Jul 25, 2019 at 17:09
  • Sample data added to original post. I stated earlier the strings that I want to identify. All other lines can be ignored. Formatting on this site always troubles me. Commented Jul 25, 2019 at 17:27
  • so you want all the lines that contain Successfully FTPed file AND contain one of DENIAL.TXT|NEWORIGIN.TXT|ORIGIN.TXT|Ed.TXT but NOT BRRef[20190724202940].txt or Ed_20190724_231907.TXT? is that correct? Commented Jul 25, 2019 at 18:05
  • Thats correct Lee. Commented Jul 25, 2019 at 18:16
  • I will later be dumping those lines into a new text file and emailing it to myself, but first I need to be able to successfully extract those lines. Commented Jul 25, 2019 at 18:16

2 Answers 2

2

presuming that you want only the 4 lines that match those 4 path\file names AND have Successfully FTPed file in the line, this seems to work. [grin]

# fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
[11:18:24 PM] Begin SFTP File \\BATCH1\TO\BRRef[20190724202940].txt To BRRef.txt
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\DENIAL.TXT[20190724194428]] to [\DENIAL.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\NEWORIGIN.TXT[20190724194428]] to [\NEWORIGIN.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\ORIGIN.TXT[20190724194428]] to [\ORIGIN.TXT]
[11:18:48 PM] Successfully FTPed file [\\BATCH1\TO\BRRef[20190724202940].txt] to [\BRRef.txt]
[11:18:48 PM] Update ProducedFile Set DeliveredFileName='BRRef.txt' Where CreateID=7710395
[11:18:48 PM] Successfully backed up file as :\\BATCH1\TO_BACKUP\BRRef[20190724202940]_20190724_231848.txt
FILE #2. [Ed.TXT[20190724194428]], begin FTP at [11:19:05 PM] 
Destination file name is: Ed.TXT[20190724194428]
After replaceing [], destination file name become: Ed.TXT
[11:19:05 PM] Begin SFTP File \\BATCH1\TO\Ed.TXT[20190724194428] To Ed.TXT
[11:19:07 PM] Successfully FTPed file [\\BATCH1\TO\Ed.TXT[20190724194428]] to [\Ed.TXT]
[11:19:07 PM] Update ProducedFile Set DeliveredFileName='Ed.TXT' Where CreateID=7710363
[11:19:07 PM] Successfully backed up file as :\\BATCH1\TO_BACKUP\Ed_20190724_231907.TXT[20190724194428]
'@ -split [System.Environment]::NewLine

$TargetOne = 'Successfully FTPed file'
$TargetTwo = @(
    '\\BATCH1\TO\DENIAL.TXT'
    '\\BATCH1\TO\NEWORIGIN.TXT'
    '\\BATCH1\TO\ORIGIN.TXT'
    '\\BATCH1\TO\Ed.TXT'
    )

<# disabled due to one server with ps3
$T2_Regex = $TargetTwo.ForEach({
    [regex]::Escape($_)
    }) -join '|'
#>
# this pipeline version otta work with ps3
$T2_Regex = ($TargetTwo |
    ForEach-Object {
    [regex]::Escape($_)
        }) -join '|'

$InStuff |
    Where-Object {
        $_ -match $TargetOne -and
        $_ -match $T2_Regex
        }

output ...

[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\DENIAL.TXT[20190724194428]] to [\DENIAL.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\NEWORIGIN.TXT[20190724194428]] to [\NEWORIGIN.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\ORIGIN.TXT[20190724194428]] to [\ORIGIN.TXT]
[11:19:07 PM] Successfully FTPed file [\\BATCH1\TO\Ed.TXT[20190724194428]] to [\Ed.TXT]
Sign up to request clarification or add additional context in comments.

10 Comments

foreach($file in $files) { if($file.LastWriteTime.ToShortDateString() -gt (get-date).AddDays(-.5)) { #Code Is Here } }
@salazar44 - what does that have to do with matching patterns in lines of text? i am confused ... [blush]
@lee_daily I guess Im confused how to integrate your code into what I already have done.
Can you change your example to a Get-Content of a file? It will help me see/undertand it better. Thanks.
use $InStuff = Get-Content -LiteralPath $FullFileName ... as my comment at the top of the answer mentioned. [grin]
|
0

It my be more prudent for you to do this via RegEx matches.

For example: Using a RegEx for multiple matches based on a simple pattern match

Clear-Host

$MyString = @'
[11:18:24 PM] Begin SFTP File \\BATCH1\TO\BRRef[20190724202940].txt To BRRef.txt
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\DENIAL.TXT[20190724194428]] to [\DENIAL.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\NEWORIGIN.TXT[20190724194428]] to [\NEWORIGIN.TXT]
[11:18:44 PM] Successfully FTPed file [\\BATCH1\TO\ORIGIN.TXT[20190724194428]] to [\ORIGIN.TXT]
[11:18:48 PM] Successfully FTPed file [\\BATCH1\TO\BRRef[20190724202940].txt] to [\BRRef.txt]
[11:18:48 PM] Update ProducedFile Set DeliveredFileName='BRRef.txt' Where CreateID=7710395
[11:18:48 PM] Successfully backed up file as :\\BATCH1\TO_BACKUP\BRRef[20190724202940]_20190724_231848.txt
FILE #2. [Ed.TXT[20190724194428]], begin FTP at [11:19:05 PM] 
Destination file name is: Ed.TXT[20190724194428]
After replaceing [], destination file name become: Ed.TXT
[11:19:05 PM] Begin SFTP File \\BATCH1\TO\Ed.TXT[20190724194428] To Ed.TXT
[11:19:07 PM] Successfully FTPed file [\\BATCH1\TO\Ed.TXT[20190724194428]] to [\Ed.TXT]
[11:19:07 PM] Update ProducedFile Set DeliveredFileName='Ed.TXT' Where CreateID=7710363
[11:19:07 PM] Successfully backed up file as :\\BATCH1\TO_BACKUP\Ed_20190724_231907.TXT[20190724194428]
'@ 

$MyRegExMatch = 'Successfully FTPed.*?TXT'
([regex]::Matches($MyString,$MyRegExMatch).Value)

# Results

<#
Successfully FTPed file [\\BATCH1\TO\DENIAL.TXT
Successfully FTPed file [\\BATCH1\TO\NEWORIGIN.TXT
Successfully FTPed file [\\BATCH1\TO\ORIGIN.TXT
Successfully FTPed file [\\BATCH1\TO\Ed.TXT
#>

Or

Clear-Host
$MyRegExMatch = 'Successfully FTPed.*?TXT'
Get-Content -Path 'variable:\MyString' | 
ForEach {([regex]::Matches($MyString,$MyRegExMatch).Value)}

7 Comments

i'm confused [blush] - why doesn't your regex match this one [\\BATCH1\TO\BRRef[20190724202940].txt]? it looks like it otta match that line.
@Postanote, this doesnt appear to filter only the 4 items I specified.
@lee_Dailey, you are right. the regex will return all lines containing 'Successfully FTPed'
@salazar44 - thank you for the confirmation! [grin]
As for this 'this doesnt appear to filter only the 4 items I specified.' but that is what my results are showing you. So, I am not sure what you mean, I guess. That is an actual run and only 4 results are given.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.