将子文件夹上移一层,包括其内容

Move Subfolder up one level including its contents

我需要移动很多子文件夹。每个client都有这个结构,需要修改

我唯一需要移动的文件夹是“Correspondence”文件夹及其所有内容。它需要“上升”一级到与“2022”文件夹相同的级别

当前文件夹结构如下所示:

客户
..........姓氏,姓名缩写
...................................2022,往年
..................................................... .....................2018
..................................................... .....................2019
..................................................... .....................2020
..................................................... .....................2021
..................................................... .....................计算
..................................................... .....................对应关系

这就是我需要的

客户
===>姓氏,姓名缩写
===>2022,通信,往年
..................................................... .....................2018
..................................................... .....................2019
..................................................... .....................2020
..................................................... .....................2021
..................................................... .........计算

这是我试过的

dir -Directory | % {Push-Location $_.FullName; dir './Correspondence' | % {Move-Item $_.FullName} 但这不起作用并导致错误

dir -Directory | % {Push-Location $_.FullName; dir './Correspondence' | % {Move-Item $_.FullName} 这也导致了错误

以下在当前目录的子树中找到所有名为 Correspondence 的子目录并将它们向上移动一级:

(Get-ChildItem -Recurse -Directory -Filter 'Correspondence') |
  Move-Item -Destination { $_.Parent.Parent.FullName } -WhatIf

注意:上面命令中的-WhatIf common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf

注:

  • Get-ChildItem 调用包含在 (...) 中,以便预先收集其所有输出,以排除移动操作干扰枚举的可能性file-system 项。

  • -Destination 参数以 delay-bind script block, which allows deriving the argument value from the input object at hand, reflected in the automatic $_ variable:

    的形式提供
    • Get-ChildItem输出的System.IO.DirectoryInfo个实例的.Parent属性反映了父目录(作为同类型实例),即子目录hand当前所在的目录。

    • .Parent.Parent因此反映子目录当前所在目录的父目录,.FullName属性包含其完整路径。