Delete all files and folders but exclude a subfolder

前端 未结 11 1162
忘掉有多难
忘掉有多难 2020-11-27 04:16

I have a folder where I need to delete all files and folders except a small list of files and folders.

I can already exclude a list of files, but don\'t see a way to

11条回答
  •  心在旅途
    2020-11-27 04:48

    I use this approach assuming you want to exclude some files or folders at root level but then you want to delete everything inside them.

    C:.
    ├───delme1
    │   │   delme.txt
    │   │
    │   └───delmetoo
    ├───delme2
    ├───folder1
    │       keepmesafe.txt
    │
    └───folder2
    

    So you want to delete everything and preserver the folder1 and folder2

    Get-ChildItem -Exclude folder1,folder2 | Remove-Item -Recurse -Force
    

    There are many other solutions but I found this easy to understand and remember.

提交回复
热议问题