¿Cómo podría desanidar varias carpetas en Windows mediante programación?

¿Cómo podría desanidar varias carpetas en Windows mediante programación?

Entonces, tengo una estructura de carpetas con algunas descargas que dice así (la edité un poco manualmente porque contiene algunos datos confidenciales, que no son relevantes para la pregunta. Las extensiones tampoco son comunes ni relevantes):

C:.
│   [FILENAME].[ext]
├───Example 1
│   └───Example 1
│       └───Example 1
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   
│           └───Folder.[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   [FILENAME].[ext]
│               │   
│               ├───test1
│               │       [FILENAME].[ext]
│               │       [FILENAME].[ext]
│               │       
│               ├───test10
│               │       [FILENAME].[ext]
│               │       [FILENAME].[ext]
│               │       
│               ├───test11
│               │       [FILENAME].[ext]
│               │       [FILENAME].[ext]
│               │       
│               ├───test12
│               │       [FILENAME].[ext]
│               │       [FILENAME].[ext]
│               │       
│               ├───test2
│                       [FILENAME].[ext]
│                       [FILENAME].[ext]
├───Example 2
│   └───Example 2
│       └───Example 2
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   
│           ├───Folder.[ext]
│           │       [FILENAME].[ext]
│           │       [FILENAME].[ext]
│           │       [FILENAME].[ext]
│           │       [FILENAME].[ext]
│           │       
│           └───Setup
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
├───Example 3
│   └───Example 3
│       └───Example 3
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   
│           └───Folder.[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
├───Example 4
│   └───Example 4
│       └───Example 4
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   [FILENAME].[ext]
│           │   
│           └───Folder.[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]
│                   [FILENAME].[ext]

                             .
                             .
                             .

Como puede ver, hay muchas carpetas innecesarias intermedias antes de los archivos reales.

¿Es posible utilizar lotes para eliminar mediante programación las "carpetas intermedias"? Básicamente, quiero eliminar las carpetas que contienen solo carpetas y subir los subcontenidos a un nivel. ¿Cómo se haría (preferiblemente sólo con las herramientas integradas de Windows)?

En este pequeño extracto, simplemente eliminar las dos primeras carpetas principales "Ejemplo #" solucionaría todo, pero la profundidad de anidamiento ocasionalmente varía hacia arriba o hacia abajo. Si una carpeta contiene solo una carpeta, es un "intermediario" que quiero eliminar; si hay archivos reales (o más de una carpeta, lo que significa que es la organización real), entonces debería permanecer allí.

¡Gracias!

EDITAR: Estoy tratando de lograr algo como esto, eliminando el anidamiento triplicado (pero a veces con otras profundidades):

C:.
├───Example 1
│    │   [FILENAME].[ext]
│    │   [FILENAME].[ext]
       .
       .
       .

Respuesta1

EnPotencia Shell, una función recursiva simple podría encontrar la primera subcarpeta que contiene archivos. Luego, el contenido de esa carpeta se puede mover a su contenedor de nivel superior. Después de eso, las carpetas vacías se pueden eliminar. Simplemente edite $Toppara reflejar la ruta a su carpeta:

$Top = 'c:\TopFolder'
Function Get-NonEmptyPath ($FPath) {
   If ( -not (gci -LiteralPath $FPath -file) ) {
      $Return = Get-NonEmptyPath ( gci -LiteralPath $FPath -Directory  ).FullName
   } Else { $Return = $FPath }
   $Return
}

Filter Get-LiteralPath { $_ | Select  @{ N='LiteralPath'; E={ $_.FullName }}}

( Get-ChildItem $Top -Directory ).FullName  | ForEach{
   If (( $Buried = Get-NonEmptyPath $_ ) -ne $null ) {
      gci -LiteralPath $Buried | Get-LiteralPath | Move-Item -Destination $_
      For ( $Empty = $Buried ; $Empty -ne $_ ; ( $Empty = Split-Path -LiteralPath $Empty )) {
         Remove-Item -LiteralPath $Empty
      }
   }
}

Después de editar $Top, el bloque completo se puede copiar y guardar en un archivoPotencia Shellventana de la consola.

gci/Get-ChildItem

Para cada

Acerca de

Mover elemento

Ruta dividida

Remover el artículo

información relacionada