Gorgon_Union
Gorgon_Union

Reputation: 583

Delete files from all subdirectories keeping folder structure except for one subdirectory

I'm trying to remove all files from all subdirectories, while keep the structure of the folders, but excluding the removal of files from the month of October 2019 in each subdirectory ⇒ root_dir\*\2019\October\

The directory structure looks like this:

C:\Users\User1\Documents\MyFolder
├───Directory1
│   ├───2018
│   │   ├───April
│   │   │   └───Error
│   │   ├───August
│   │   │   └───Error
│   │   ├───February
│   │   │   └───Error
│   │   ├───January
│   │   │   └───Error
│   │   ├───July
│   │   │   └───Error
│   │   ├───June
│   │   │   └───Error
│   │   ├───March
│   │   │   └───Error
│   │   ├───May
│   │   │   └───Error
│   │   ├───October
│   │   │   └───Error
│   │   └───September
│   │       └───Error
│   └───2019
│       ├───April
│       │   └───Error
│       ├───August
│       │   └───Error
│       ├───February
│       │   └───Error
│       ├───January
│       │   └───Error
│       ├───July
│       │   └───Error
│       ├───June
│       │   └───Error
│       ├───March
│       │   └───Error
│       ├───May
│       │   └───Error
│       ├───October
│       │   └───Error
│       └───September
│           └───Error
└───Directory2
    ├───2018
    │   ├───April
    │   │   └───Error
    │   ├───August
    │   │   └───Error
    │   ├───February
    │   │   └───Error
    │   ├───January
    │   │   └───Error
    │   ├───July
    │   │   └───Error
    │   ├───June
    │   │   └───Error
    │   ├───March
    │   │   └───Error
    │   ├───May
    │   │   └───Error
    │   ├───October
    │   │   └───Error
    │   └───September
    │       └───Error
    └───2019
        ├───April
        │   └───Error
        ├───August
        │   └───Error
        ├───February
        │   └───Error
        ├───January
        │   └───Error
        ├───July
        │   └───Error
        ├───June
        │   └───Error
        ├───March
        │   └───Error
        ├───May
        │   └───Error
        ├───October
        │   └───Error
        └───September
            └───Error

From Microsoft PowerShell docs I should be able to wildcard the exclude path. I've tried a few variations but here is where I'm currently at:

$root     = 'C:\Users\User1\Documents\MyFolder'
$excludes = 'C:\Users\User1\Documents\MyFolder\*\2019\October\'

Get-ChildItem $root -Directory -Exclude $excludes | ForEach-Object {
    Get-ChildItem $_.FullName -File -Recurse -Force | Remove-Item -Force
}

The above works except its still removing files from C:\Users\User1\Documents\MyFolder\Directory1\2019\October\* and C:\Users\User1\Documents\MyFolder\Directory2\2019\October\*

I've tried specifying .\2019\October\*.* but that doesn't seem to work either.

Upvotes: 1

Views: 115

Answers (1)

mklement0
mklement0

Reputation: 437743

I suggest a different approach:

$root     = 'C:\Users\User1\Documents\MyFolder'
$excludes = '*\2019\October\*'

Get-ChildItem $root -File -Recurse |
  Where-Object FullName -notlike $excludes |
    Remove-Item -Force -WhatIf

-WhatIf previews the removal operation; remove it to perform actual removal.

For simplicity, all files in the subtree are enumerated, and then filtered out by whether their full paths contain \2019\October\ path components.


As for what you tried:

$excludes = 'C:\Users\User1\Documents\MyFolder\*\2019\October\'

The -Exclude parameter only supports file name patterns, not full paths - though there is a pending feature request on GitHub to add support for paths.

Also, using Get-ChildItem -Exclude without -Recurse doesn't work the way one would expect: the exclusion is then applied to the input path only - see this GitHub issue.

Upvotes: 2

Related Questions