Powershell 将文件夹移动到父文件夹和apppend父名称

Powershell 将文件夹移动到父文件夹和apppend父名称,powershell,Powershell,我的文件夹遵循以下模式: C:\ └───root ├───V16 │ ├───1 │ │ A.sql │ │ B.sql │ │ │ ├───2 │ │ C.sql │ │ │ └───4 │ E.sql │ └───V17 └───1 D.sql 我的目标是获得

我的文件夹遵循以下模式:

C:\
└───root
    ├───V16
    │   ├───1
    │   │       A.sql
    │   │       B.sql
    │   │
    │   ├───2
    │   │       C.sql
    │   │
    │   └───4
    │           E.sql
    │
    └───V17
        └───1
                D.sql
我的目标是获得这种模式:

C:\root\V16-1\A.sql
C:\root\V16-1\B.sql
C:\root\V16-2\C.sql
C:\root\V17-1\D.sql
C:\root\V17-4\E.sql

我完全知道如何在C#中实现这一点,但不幸的是,我在powershell中是个新手

移动文件夹时,*.sql文件是不相关的

Get-ChildItem C:\root\*\* -Directory |
  Move-Item -dest {'{0}\{1}-{2}' -f $_.PSParentPath.replace('\'+$_.Parent,''),$_.Parent,$_.Name} -WhatIf
如果输出看起来正常,请移动尾随的
-WhatIf

生成的树示例:

C:\
└───root
    ├───V16
    ├───V16-1
    │       A.sql
    │       B.sql
    │
    ├───V16-2
    │       C.sql
    │
    ├───V16-4
    │       E.sql
    │
    ├───V17
    └───V17-1
            D.sql
试试这个

$DirToRemove=@()

Get-ChildItem "C:\temp\root\*\*" -directory | %{
    move-Item $_.FullName ($_.Parent.FullName + '-' + $_.Name)
    $DirToRemove+=$_.Parent.FullName
}

$DirToRemove | select -Unique | Remove-Item