为什么Powershell的Move-Item删除目标目录? [英] Why is Powershell's Move-Item deleting the destination directory?

查看:1072
本文介绍了为什么Powershell的Move-Item删除目标目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要替换文件名中的方括号,并且已经成功地创建了(并在控制台上验证)一个 -replace 。现在我试图将 Move-Item 添加到新目录,因为这个bug阻止我做一个简单的文件重命名。



这是我的脚本:

  $ 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'\ {|}','_')
}

发生了什么:我正在使用 $ txtPath2 变量,但是不断得到无法绑定到null目录的错误,所以我明确地编码到路径,看看是否有一些奇怪的变量如何解析。现在,我收到这个错误:

 移动项:不能移动项目,因为'C:\users\ xxxxxx\desktop\test001'不存在。 
在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

这里是奇怪的是,我创建了新的目录。我运行的脚本,看着它从我的桌面消失,脚本失败。 WTF?我已经退出并重新启动控制台应用程序来刷新任何变量。我在 Move-Item 行中尝试了不同风格的变量与常量。除非我缺少一个 Move-Item 参数,否则我真的不知道发生了什么。有没有人看到会导致我的文件被删除的任何东西?



编辑:
编辑到



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

我收到一个新错误:

 异常使用2参数调用Move:空文件名不合法
参数名称:destFileName
在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


解决方案

您设置了 $ _。Name as destination(second arg)。 名称只是一个项目的FILE名称,所以我猜你的 test001 文件/文件夹被移动到您运行脚本的地方,并重命名为任何 $ _。Name was(它使用Name作为相对路径)。因此,如果您从 c:\windows\system32 (当PS以管理员身份运行时的默认文件夹)运行此脚本,则将其移动到该位置。



下一次在您的 foreach -loop中, test001 已被移动,返回错误。 -LiteralPath 是源位置,而不是目的地。



尝试:

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


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

解决方案

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 '\[|\]', '') ) }

这篇关于为什么Powershell的Move-Item删除目标目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆