我有一个脚本(粘贴在下面)应该做到以下几点:
它正在执行步骤#1和#2,但步骤#3,移动项目不移动任何文件,我不知道为什么,我没有得到一个错误的迹象
有人可以帮忙吗? 谢谢
$source = "C:\Users\Tom\" $source_move = "C:\Users\Tom\OldLogBackup1\" $destination ="C:\Users\Tom\Mirror\" if(-not(Test-Path $destination)){mkdir $destination | out-null} #Step 1 ForEach ($sourcefile In $(Get-ChildItem $source | Where-Object { $_.Name -match "Daily_Reviews\[\d\d\d\d-\d\d\d\d\].journal" })) { #escape filename because there are brackets in the name! $src = [Management.Automation.WildcardPattern]::Escape($sourcefile) #Step 2 Copy-Item $src $destination ### Check for a successful file copy if($?) { if(-not(Test-Path $source_move)){ echo "Path does not exist!" } else { echo "Path exists!" ### Step 3 ### Move original source file someplace else Move-Item $source_move$sourcefile $source_move if($?) { echo "Source file successfully moved" } else { echo "Source file was not successfully moved" } } }
首先,看起来像将文件从$ source_move移动到$ source_move所在的目录。 我想你想看起来像这样:
Move-Item $source + $sourcefile $source_move
另外,尝试在$ source_move和$ sourcefile之间加上+。
这正是你的问题。 你不能像这样链接variables。 PoSh会感到困惑。
尝试这个:
PS C:\> $a = "Powershell" PS C:\> $b = " Rocks!" PS C:\> $a Powershell PS C:\> $b Rocks! PS C:\> $a$b At line:1 char:3 + $a$b + ~~ Unexpected token '$b' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken PS C:\> $a+$b Powershell Rocks! PS C:\> "$a$b" Powershell Rocks!
并且您正在尝试移动到相同的文件夹(source = destination)。 这总是一个不是。