Move-Item在尝试将目录内容移动到同一目录中的子文件夹时抛出错误。

huangapple go评论71阅读模式
英文:

Move-Item throws error when attempting to move directory contents to subfolder within same directory

问题

我有一个包含文件、文件夹和目标子文件夹的文件夹。我尝试使用Move-Item将文件夹的所有内容移动到目标子文件夹中(不包括目标子文件夹),但当我这样做时,我收到了意外的“不存在”错误。我是否误用了该命令?是否有其他参数可以实现我想要的效果?

PS C:\test> dir

    目录: C:\test

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         6/14/2023   1:06 PM                anotherFolder
d-----         6/14/2023  12:50 PM                subfolder
-a----         6/14/2023  12:50 PM              6 ItemA
-a----         6/14/2023  12:50 PM              6 ItemB
-a----         6/14/2023  12:50 PM              6 ItemC

PS C:\test> Move-Item * -Destination subfolder -Exclude subfolder
Move-Item : 无法移动项目因为位于 'C:\test\subfolder' 处的项目不存在
At line:1 char:1
+ Move-Item * -Destination subfolder -Exclude subfolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand

PS C:\test> dir

    目录: C:\test

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         6/14/2023  12:50 PM                subfolder

看起来移动成功了,但我希望在简单移动操作时不会出现错误...如果我尝试不使用Exclude参数,我会收到不同的错误:

PS C:\test> Move-Item * -Destination subfolder
Move-Item : 参数无效
At line:1 char:1
+ Move-Item * -Destination subfolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\test\subfolder:DirectoryInfo) [Move-Item], IOException
    + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

PS C:\test>
英文:

I have a folder containing files, folders, and a target subfolder. I'm trying to move all contents of the folder into the target subfolder (excluding the target subfolder) using Move-Item. When I do so I get an unexpected "does not exist" error for the target subfolder. Am I misusing the command? Is there another argument that achieves what I want?

PS C:\test> dir


    Directory: C:\test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         6/14/2023   1:06 PM                anotherFolder
d-----         6/14/2023  12:50 PM                subfolder
-a----         6/14/2023  12:50 PM              6 ItemA
-a----         6/14/2023  12:50 PM              6 ItemB
-a----         6/14/2023  12:50 PM              6 ItemC


PS C:\test> Move-Item * -Destination subfolder -Exclude subfolder
Move-Item : Cannot move item because the item at 'C:\test\subfolder' does not exist.
At line:1 char:1
+ Move-Item * -Destination subfolder -Exclude subfolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand

PS C:\test> dir


    Directory: C:\test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         6/14/2023  12:50 PM                subfolder

Looks like the move succeeded but I would prefer to not get an error for simple move operation... if I try without the Exclude argument, I get a different error:

PS C:\test> Move-Item * -Destination subfolder
Move-Item : The parameter is incorrect.
At line:1 char:1
+ Move-Item * -Destination subfolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\test\subfolder:DirectoryInfo) [Move-Item], IOException
    + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

PS C:\test>

答案1

得分: 1

您看到的是Windows PowerShell中的一个**错误**,已在PowerShell (Core) 7+中修复。

解决方法

Get-Item * -Exclude subfolder | Move-Item -Destination subfolder

注意:要包括_隐藏_文件和子文件夹,请在Get-Item调用中添加-Force

  • 在这种情况下,Get-ChildItem *也可以工作,但Get-Item *在各种情况下更加可预测。

  • 请注意,为了使-Exclude参数按预期工作(换句话说:仅Get-ChildItem -Exclude subfolder将不起作用),您必须使用*,这是因为-Include-Exclude参数的实现方式与直觉相反 - 请参阅此答案以获取背景信息。

英文:

You're seeing a bug in Windows PowerShell, which has been fixed in PowerShell (Core) 7+.

Workaround:

Get-Item * -Exclude subfolder | Move-Item -Destination subfolder

<sup>Note: To include hidden files and subfolders, add -Force to the Get-Item call.</sup>

  • Get-ChildItem * would work too in this case, but Get-Item * works more predictably across various scenarios.

  • Note that you must use * in order for the -Exclude parameter to work as intended (in other words: just Get-ChildItem -Exclude subfolder would not work), because of the counterintuitive way the -Include and -Exclude parameters are implemented - see this answer for background information.

huangapple
  • 本文由 发表于 2023年6月15日 04:04:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定