24

I'm trying to move all the mails after removing the special characters in the filename to some destination based on the filename.

FOLDLIST is an array, where I'm having the condition variable and destination foldername.

Set-Location 'C:\Users\abrahame\Desktop\Work\PSG Mail Movement\Mail'
$DESLOC="c:\Temp\ua-closed bugs"
$FOLDLIST = @(("UA", "CLOSE",  "ua-closed bugs"), ("VS", "CLOSE", "vs-closed-bugs"), ("CM", "CLOSED", "cm - closed-bugs"))
gci | Foreach-object { $NEWN = $_.Name -replace '&',' ' -replace '_', ' ' -replace '#', ' ' -replace '!', ' '  -replace '@', ' '  -replace '$', ' '  -replace '%', ' '  -replace '^', ' '  -replace '&', ' '  -replace '\(', ' ' -replace '\)', ' '  -replace '\[', ' ' -replace '\]', ' ' -replace '\{', ' '  -replace '\}', ' ' -replace '\-', ' ';
    Write-Host $NEWN.Length
    if($NEWN.Length -gt  70) {
        $NEWN="$NEWN.Substring(1,70)"
        $NEWN=$NEWN.msg
    }

    $FOLDLIST | ForEach-Object {
        $CXR=$_[0]
        $STAT=$_[1]

        if ($NEWN -match ("$CXR") -and $NEWN -match ("$STAT")){
            Write-Host  $CXR -  $STAT
            $DIR=$_[2]
            $NEWN=$NEWN.trim()
            $DPATH="$DESLOC\$DIR\$NEWN"
            Write-Host $DPATH
            mv $_.Name $DPATH
        }
    }
}

I'm getting this error. Where did I make a mistake?

67
UA - CLOSE
c:\Temp\ua-closed bugs\ua-closed bugs\RE  CLOSE OA TICKET   10350   OA   UAT PHASE FOR HP FARES  1 .msg
Move-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\abrahame\Desktop\Work\PSG Mail Movement\mailmove_multdimentional.ps1:24 char:5
+         mv <<<<  $_.Name $DPATH
    + CategoryInfo          : InvalidData: (:) [Move-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.MoveItemCom
   mand
1

2 Answers 2

9

My guess is that $_.Name does not exist.

If I were you, I'd bring the script into the ISE and run it line for line till you get there then take a look at the value of $_.

Sign up to request clarification or add additional context in comments.

1 Comment

In my case, this was true and thus using conditional logic such as If($var){$var | % {Remove-Item -Path $_.FullName -Recurse -Force -Confirm:$False}};. So it'll only take this action is that $var is true and if it's not true or null, it'll still move on after the conditional logic or whatever.
3

$_ is the active object in the current pipeline. You've started a new pipeline with $FOLDLIST | ... so $_ represents the objects in that array that are passed down the pipeline. You should stash the FileInfo object from the first pipeline in a variable and then reference that variable later e.g.:

write-host $NEWN.Length
$file = $_
...
Move-Item $file.Name $DPATH

2 Comments

Keith, I'm doing the same as you advised. $CXR=$_[0] $STAT=$_[1] and $DIR=$_[2] . I'm taking the information and storing in these variables and using that..
Thank you Very much.. It is because of first for loop. You are right.

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.