Wie kann ich in Windows programmgesteuert die Verschachtelung mehrerer Ordner aufheben?

Wie kann ich in Windows programmgesteuert die Verschachtelung mehrerer Ordner aufheben?

Ich habe also eine Ordnerstruktur mit einigen Downloads, die wie folgt aussieht (ich habe sie manuell ein wenig bearbeitet, da sie einige vertrauliche Daten enthält, die für die Frage nicht relevant sind. Die Erweiterungen sind auch weder üblich noch relevant):

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]

                             .
                             .
                             .

Wie Sie sehen, liegen vor den eigentlichen Dateien jede Menge unnötige Zwischenordner.

Ist es möglich, die „Zwischenordner“ programmgesteuert per Batch zu entfernen? Grundsätzlich möchte ich alle Ordner entfernen, die nur Ordner enthalten, und die Unterinhalte eine Ebene nach oben bringen. Wie würde das gehen (vorzugsweise nur mit integrierten Windows-Tools)?

In diesem kleinen Auszug würde das Entfernen der ersten beiden übergeordneten Ordner „Beispiel #“ alle Probleme lösen, aber die Verschachtelungstiefe variiert gelegentlich nach oben oder unten. Wenn ein Ordner nur einen einzigen Ordner enthält, ist es ein „Mittelsmann“, den ich entfernen möchte. Wenn tatsächlich Dateien vorhanden sind (oder mehr als ein Ordner, was bedeutet, dass es sich um eine tatsächliche Organisation handelt), sollte er dort bleiben.

Danke schön!

BEARBEITEN: Ich versuche, so etwas zu erreichen, indem ich die dreifache (manchmal aber auch andere Tiefe) Verschachtelung entferne:

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

Antwort1

InPower Shell, eine einfache rekursive Funktion könnte den ersten Unterordner finden, der Dateien enthält. Der Inhalt dieses Ordners kann dann in seinen Container der obersten Ebene verschoben werden. Danach können leere Ordner gelöscht werden. Bearbeiten Sie einfach, $Topum den Pfad zu Ihrem Ordner anzuzeigen:

$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
      }
   }
}

Nach der Bearbeitung $Topkann der gesamte Block kopiert und in einPower ShellKonsolenfenster.

gci/Get-ChildItem

Für jede

Über For

Artikel verschieben

Geteilter Pfad

Gegenstand entfernen

verwandte Informationen