Why is Powershell's Move-Item deleting the destination directory?

末鹿安然 提交于 2019-12-11 18:53:42

问题


I need to replace square brackets in filenames, and I've successfully created (and validated at the console) a -replace. Now I'm trying to Move-Item to a new directory because this bug in Powershell 2.0 prevents me from doing a simple file rename.

Here's my script:

$txtPath = "c:\users\xxxxxx\desktop\cgc\tx"     #source files
$txtPath2 = "c:\users\xxxxxx\desktop\cgc\tx2"   #renamed files
Get-ChildItem $txtPath | foreach { 
    Move-Item -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_') 
}

Here's what's happening: I was using the $txtPath2 variable, but kept getting "cannot bind to null directory" errors, so I explicitly coded to the path to see if there was something odd with how the variable parsed. Now, I get this error:

Move-Item : Cannot move item because the item at 'C:\users\xxxxxx\desktop\test001' does not exist.
At C:\users\xxxxxx\desktop\cgc\rni.ps1:5 char:10
+ Move-Item <<<<  -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_')
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand

Here's what's odd: I create the new directory. I run the script, watch it vanish from my desktop as the script fails. WTF? I've quit and restarted the console application to flush any variables. I've tried different flavors of variable vs. constant in the Move-Item line. Unless there's a Move-Item parameter that I'm missing, I really have no idea what's going on. Does anyone else see anything that would cause my file to be deleted?

EDIT: After editing to

Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($i.FullName -replace '\[|\]', '') ) }

I get a new error:

Exception calling "Move" with "2" argument(s): "Empty file name is not legal.
Parameter name: destFileName"
At C:\users\x46332\desktop\cgc\rni.ps1:6 char:52
+ Get-ChildItem $txtPath | % { [system.io.file]::Move <<<< ($_.fullname, ($i.FullName -replace '\[|\]', '') ) }
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

回答1:


You set a modified version of $_.Name as destination (second arg). "Name" is just the FILEname of an item, so I'm guessing your test001 file/folder got moved to the place you run the script from and renamed to whatever $_.Name was (it uses Name as a relative path). So if you run this script from c:\windows\system32 (default folder when PS is running as admin), you move it there.

The next time in your foreach-loop, test001 is already moved and it returns an error. -LiteralPath is source location, not destination.

Try:

Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($_.FullName -replace '\[|\]', '') ) }


来源:https://stackoverflow.com/questions/14608614/why-is-powershells-move-item-deleting-the-destination-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!