1
votes

This one alone does work. If I try HandleReport -ReportName "123456.pdf" it says matches

function HandleReport 
{
  param ( [string] $ReportName = "" )

       if ( $ReportName  -match "(\d{6})." ) {
         $Result = $_ + " matches ..."
         write $Result
       }
}

Now I call it from a function which reads a directory:

for ($i=0; $i -lt $EventTypes.length; $i++) {
    # $FU_Dir is correct and does exist.
    dir $FU_Dir | foreach {
       $ReportName = $_.name
       HandleReport -ReportName $ReportName
   }
}   

Now I get:

Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'op_Addition'. At line:6 char:24 + $Result = $_ + <<<< " matches ..." + CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound matches ...

maybe the $_.name does not give back a string?

1
"(\d{6})." ... Did you want 6 digits followed by a period? You should use "(\d{6})\." so you have a literal period instead of any character.Matt
thx a lot, now its working !!!Eryk

1 Answers

2
votes

I'm not sure what the purpose of $_ is in the function. You already pass a string to the function so it should work if you change it to:

function HandleReport 
{
  param ([string] $ReportName = "")

       if ($ReportName -match "(\d{6})." ) {
         $Result = "$ReportName matches ..."
         Write-Output $Result
       }
}