What Im doing wrong?

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Anthon Hassel Anthon Hassel 2 years, 2 months ago.

  • Author
    Posts
  • #18616
    Profile photo of Anthon Hassel
    Anthon Hassel
    Participant

    Hello!

    I have created a script that should sort specific e-mails into different folders. I have an array called $searchwords with all the e-mail adresses and the script works well exept that if for some reason skips the first mail in my mail folder and copies it to the Unknown folder. So for some reason the script goes to the "else" in the first loop.. Anyone know why?? If I remove the else code the script run without any problem.

    Folder structure:
    1. Mail (Contains alot of mail files with the file extension .mml)
    2. WantedMail (Directory that will get all the sorted emails)
    3. Script.ps1

    Here is the code:

    $mailfolder = "Mail"
    $copiedmail = "WantedMail"
    [int]$loop = 1

    $searchwords = @("test@mail.com","test1@mail.com")

    foreach ($x in (cmd /c dir $mailfolder /b))
    {
    Write-host $loop -ForegroundColor Yellow
    $loop++
    foreach ($word in $searchwords)
    {
    if (Get-Content -Encoding UTF8 $mailfolder\$x -ReadCount 1000 | Where-Object {$_ -match $word})
    {
    Write-host $word -ForegroundColor green
    if(!(Test-Path -Path $copiedmail\$word))
    {
    New-item -ItemType Directory -Path "$copiedmail\$word"
    Copy-item -Path "$mailfolder\$x" "$copiedmail\$word" -Force
    }
    elseif (Test-Path -Path $copiedmail\$word)
    {
    Copy-item -Path "$mailfolder\$x" "$copiedmail\$word" -Force
    }

    }
    else
    {
    if(!(Test-Path -Path "$copiedmail\Unknown"))
    {
    New-item -ItemType Directory -Path "$copiedmail\Unknown"
    Copy-item -Path "$mailfolder\$x" "$copiedmail\Unknown" -Force
    }
    Write-host "$word was not found in $x" -ForegroundColor Red
    }
    }
    }

  • #18618
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    You're using Get-Content which will look into a file for info. Since you've got tons of files that you're matching based on names, you should use Get-ChildItem, which will list out files.

  • #18623
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You have multiple search words, but you're copying the file the first time through your foreach ($word in $searchwords) loop without checking to see if the second word is in it. Based on what I can see of your code, it's possible that the file that's being "skipped" contains test1@mail.com but not test@mail.com . It's also possible that the file is not UTF8 encoded, which would mess up your code.

    I've revised your code slightly so it checks each file for each word, and only copies the file to Unknown if no matches were found. I also ditched Get-Content and Where-Object in favor of using Select-String instead, which is faster:

    $mailfolder = "Mail"
    $copiedmail = "WantedMail"
    [int]$loop = 1
    
    $searchwords = @("test@mail.com","test1@mail.com")
    
    foreach ($x in (cmd /c dir $mailfolder /b))
    {
        Write-host $loop -ForegroundColor Yellow
        $loop++
        
        $foundMatch = $false
        
        foreach ($word in $searchwords)
        {
            if (Select-String -Path $mailfolder\$x -Pattern $word -Quiet -Encoding UTF8 -SimpleMatch)
            {
                $foundMatch = $true
                Write-host $word -ForegroundColor green
                
                if(!(Test-Path -Path $copiedmail\$word))
                {
                    New-item -ItemType Directory -Path "$copiedmail\$word"
                }
                
                Copy-item -Path "$mailfolder\$x" "$copiedmail\$word\" -Force
            }
            else
            {
                Write-host "$word was not found in $x" -ForegroundColor Red
            }
        }
        
        if (!$foundMatch)
        {
            if(!(Test-Path -Path "$copiedmail\Unknown"))
            {
                New-item -ItemType Directory -Path "$copiedmail\Unknown"
            }
        }
        
        Copy-item -Path "$mailfolder\$x" "$copiedmail\Unknown\" -Force
    }
    
  • #18636
    Profile photo of Anthon Hassel
    Anthon Hassel
    Participant

    Thank you Stephen Owen for the tips and thank you Dave Wyatt for the help with the script. It work's perfect!

You must be logged in to reply to this topic.