NDC
NDC

Reputation: 29

How do I fix "positional parameter cannot be found that accepts "+""?

So I have this task in which I have to read a file. Then count how many times every word in the file occurs. After that every single word must be written to an additional file with behind that specific word the number of how many times it occurs.

I have some code already but I'm running into errors with everything I try, I'm new to this and I don't understand a lot of the error messages.

Function AnalyseTo-Doc
{
    param ([Parameter(Mandatory=$true)][string]$Pad )
    New-Item C:\destination.txt -ItemType file
    $destination = "C:\destination.txt"
    $filecontents = get-content $Pad | Out-String

    foreach($hit in $filecontents)
    {
        #$tempWoord = $hit | Convert-String
        $lengte = $hit.Length
        if($lengte -ge 4)
        {
            $hits = get-content $Pad | Out-String
            if($hits -notcontains $hit)
            {
                Add-Content $destination $hit + $hit.LineNumber + '`n'
            }
            elseif($hits -contains $hit)
            {
                Add-Content $destination $hit + $hit.LineNumber + '`n'
            }
        }
    }
}

So as said above, what this has to do is:

  1. It has to read the file properly.
  2. It has to know if words are over 4 characters. If they are, they must be counted
  3. Every word of over 4 chars must be counted.
  4. Last, every single word must be written to an additional file where it says the word itself and a count behind it.

By count I mean: how many times it appears in the text file

PS: We're testing with .txt files

Upvotes: 1

Views: 14356

Answers (2)

AdminOfThings
AdminOfThings

Reputation: 25001

I know this question already has an answer, but if we are only counting consecutive alpha characters as words and you want the total count of those words, this should work provided there are no character exceptions. The post in the question doesn't seem to actually count words.

Function AnalyseTo-Doc
{
    param ([Parameter(Mandatory=$true)][string]$Pad )

    New-Item C:\destination.txt -ItemType file
    $destination = "C:\destination.txt"
    $filecontents = Get-Content $Pad -Raw

    $words = ($filecontents | Select-String -Pattern "\b[A-Za-z]{4,}\b" -AllMatches).Matches.Value
    $words | Group-Object -NoElement | Foreach-Object {
        ("{0},{1}" -f $_.Count,$_.Name) | Add-Content -Path $destination
        }
}

Upvotes: 1

Robert Dyjas
Robert Dyjas

Reputation: 5227

NOTE: As the question is How do i fix “positional parameter cannot be found that accepts ”+“, let me answer exactly that one. The answer below doesn't address other issues which might appear once you fix the below.


What you should see in your error message is something like:

PS C:\SO\56526906> Add-Content 'destination.txt' $a + $b + $c
Add-Content : A positional parameter cannot be found that accepts argument '+'.
At line:1 char:1
+ Add-Content 'destination.txt' $a + $b + $c
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand

Which points you directly to the line where the error occurs. In that case, it's because you're giving the following parameters to Add-Content:

$destination
$hit
+
$hit.LineNumber
+
'`n'

While you should add only destination and content. Your invocation of Add-Content should look like this:

Add-Content $destination "$hit $($hit.LineNumber)"

Please notice that you don't need to add `n after Add-Content as newlines will be added automatically.

Upvotes: 0

Related Questions