I’m new to powershell. I am calling a function I made and It’s separating my two parameters in to several more. How do I make the calculated parameter actually be calculated before it’s passed?
dir -Recurse -Path $source | foreach {MoveFile $_.FullName ($destination + "\" + $_.Extension.Substring(1, ($_.Extension.Length - 1))) }
It’s this part:
($destination + "\" + $_.Extension.Substring(1, ($_.Extension.Length - 1))
That has problems. It separates it rather than calculates it. What am I doing wrong?
You are doing it correctly by putting the argument expression in parens. When I use that snippet with Copy-Item on my machine, it works just fine. You might need to show what your MoveFile function looks like. It could be that in your case $_.Extension is evaluating to $null. It doesn’t appear your are filtering for files that have an extension.
BTW, with the built-in Move-Item you could simplify this to:
There is an overload of
String.Substringthat takes just the start index and gives you the rest of the string from that point to the end. So you don’t have to specify the length of the rest of the string.