Delete all files and folders but exclude a directo

2019-01-07 10:05发布

I have a diectory 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 dont see a way to exclude a folder.

Here is the folder structure:

|-C:\temp
 \-C:\temp\somefile.txt
 \-C:\temp\someotherfile.txt
| |-C:\temp\foldertodelete
   \-C:\temp\foldertodelete\file1.txt
| |-C:\temp\foldertokeep
|  \-C:\temp\foldertokeep\file2.txt

I want to keep somefile.txt and the folder foldertokeep and its content.

This is what I have right now:

Get-ChildItem -Path  'C:\temp' -Recurse -exclude somefile.txt | Remove-Item -force -recurse

This realy does not delete somefile.txt. Is there a way to exclude folder foldertokeep and its content from the delete list?

标签: powershell
10条回答
The star\"
2楼-- · 2019-01-07 10:18

According to MSDN Remove-Item has a known issue with the -exclude param. Use this variant instead.

Get-ChildItem * -exclude folderToExclude | Remove-Item
查看更多
Rolldiameter
3楼-- · 2019-01-07 10:18

This would also help someone...

Get-ChildItem -Path PATH_GOES_HERE -Recurse -Exclude "Folder1 ", "Folder2", FileName.txt | foreach ($_) {
    "CLEANING :" + $_.fullname
    Remove-Item $_.fullname -Force -Recurse
    "CLEANED... :" + $_.fullname
}
查看更多
倾城 Initia
4楼-- · 2019-01-07 10:20

I ran into this and found a one line command that works for me. It will delete all the folders and files on the directory in question, while retaining anything on the "excluded" list. It also is silent so it won't return an error if some files are read-only or in-use.

@powershell Remove-item C:\Random\Directory\* -exclude "MySpecialFolder", "MySecondSpecialFolder" -force -erroraction 'silentlycontinue'
查看更多
beautiful°
5楼-- · 2019-01-07 10:25

This would also help someone...

Adding a variable for PATH_GOES_HERE that is empty or isn't defined prior can cause a recursive deletion in the user directory (or C:\windows\system32 if the script is ran as admin). I found this out the hard way and had to re-install windows.

Try it yourself! (below will only output the file directories into a test.txt)

Get-ChildItem -Path $dir2 -Recurse -Exclude "Folder1 ", FileName.txt | foreach ($_) {
$_.fullname >> C:\temp\test.txt
}
查看更多
何必那么认真
6楼-- · 2019-01-07 10:26
Get-ChildItem -Path  'C:\temp' -Recurse -exclude somefile.txt |
Select -ExpandProperty FullName |
Where {$_ -notlike 'C:\temp\foldertokeep*'} |
sort length -Descending |
Remove-Item -force 

The -recurse switch does not work properly on Remove-Item (it will try to delete folders before all the child items in the folder have been deleted). Sorting the fullnames in descending order by length insures than no folder is deleted before all the child items in the folder have been deleted.

查看更多
够拽才男人
7楼-- · 2019-01-07 10:27

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.

查看更多
登录 后发表回答