删除所有文件和文件夹但排除目录

时间:2021-12-12 02:23:02

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.

我想保留somefile.txt和文件夹foldertokeep及其内容。

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?

这真的不会删除somefile.txt。有没有办法从删除列表中排除文件夹foldertokeep及其内容?

9 个解决方案

#1


39  

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.

-recurse开关在Remove-Item上无法正常工作(它将尝试在删除文件夹中的所有子项之前删除文件夹)。按长度降序对全名进行排序可确保在删除文件夹中的所有子项之前不删除任何文件夹。

#2


21  

In PowerShell 3.0 and below, you can try simply doing this:

在PowerShell 3.0及更低版本中,您可以尝试简单地执行此操作:

Remove-Item -recurse c:\temp\* -exclude somefile.txt,foldertokeep

Unless there's some parameter I'm missing, this seems to be doing the trick...

除非我缺少一些参数,否则这似乎就是诀窍......

Edit: see comments below, the behavior of Remove-Item has changed after PS3, this solution doesn't seem applicable anymore.

编辑:看下面的评论,删除项目的行为在PS3之后发生了变化,此解决方案似乎不再适用。

#3


6  

I used the below and just removed -Recurse from the 1st line and it leaves all file and sub folders under the exclude folder list.

我使用下面的内容并从第一行中删除-Recurse,它将所有文件和子文件夹保留在排除文件夹列表下。

   Get-ChildItem -Path "PATH_GOES_HERE" -Exclude "Folder1", "Folder2", "READ ME.txt" | foreach ($_) {
       "CLEANING :" + $_.fullname
       Remove-Item $_.fullname -Force -Recurse
       "CLEANED... :" + $_.fullname
   }

#4


3  

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'

#5


2  

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.

为PATH_GOES_HERE添加一个空的或之前未定义的变量可能会导致在用户目录中进行递归删除(如果脚本以管理员身份运行,则会导致C:\ windows \ system32)。我发现这很困难,不得不重新安装Windows。

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

亲自尝试一下! (下面只会将文件目录输出到test.txt中)

Get-ChildItem -Path $dir2 -Recurse -Exclude "Folder1 ", FileName.txt | foreach ($_) {
$_.fullname >> C:\temp\test.txt
}

#6


1  

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.

我使用这种方法假设您想要在根级别排除某些文件或文件夹,但是您想要删除它们内部的所有内容。

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

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

还有很多其他解决方案,但我发现这很容易理解和记住。

#7


0  

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
}

#8


0  

I used this, that works perfectly for me

我用过这个,对我来说很有效

Get-ChildItem -Path 'C:\Temp\*' -Recurse | Where-Object {($_.FullName -notlike "*windirstat*") -and ($_.FullName -notlike "C:\Temp\GetFolderSizePortable*")} | Remove-Item -Recurse

#9


0  

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

根据MSDN Remove-Item与-exclude参数有一个已知问题。请改用此变体。

Get-ChildItem * -exclude folderToExclude | Remove-Item

#1


39  

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.

-recurse开关在Remove-Item上无法正常工作(它将尝试在删除文件夹中的所有子项之前删除文件夹)。按长度降序对全名进行排序可确保在删除文件夹中的所有子项之前不删除任何文件夹。

#2


21  

In PowerShell 3.0 and below, you can try simply doing this:

在PowerShell 3.0及更低版本中,您可以尝试简单地执行此操作:

Remove-Item -recurse c:\temp\* -exclude somefile.txt,foldertokeep

Unless there's some parameter I'm missing, this seems to be doing the trick...

除非我缺少一些参数,否则这似乎就是诀窍......

Edit: see comments below, the behavior of Remove-Item has changed after PS3, this solution doesn't seem applicable anymore.

编辑:看下面的评论,删除项目的行为在PS3之后发生了变化,此解决方案似乎不再适用。

#3


6  

I used the below and just removed -Recurse from the 1st line and it leaves all file and sub folders under the exclude folder list.

我使用下面的内容并从第一行中删除-Recurse,它将所有文件和子文件夹保留在排除文件夹列表下。

   Get-ChildItem -Path "PATH_GOES_HERE" -Exclude "Folder1", "Folder2", "READ ME.txt" | foreach ($_) {
       "CLEANING :" + $_.fullname
       Remove-Item $_.fullname -Force -Recurse
       "CLEANED... :" + $_.fullname
   }

#4


3  

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'

#5


2  

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.

为PATH_GOES_HERE添加一个空的或之前未定义的变量可能会导致在用户目录中进行递归删除(如果脚本以管理员身份运行,则会导致C:\ windows \ system32)。我发现这很困难,不得不重新安装Windows。

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

亲自尝试一下! (下面只会将文件目录输出到test.txt中)

Get-ChildItem -Path $dir2 -Recurse -Exclude "Folder1 ", FileName.txt | foreach ($_) {
$_.fullname >> C:\temp\test.txt
}

#6


1  

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.

我使用这种方法假设您想要在根级别排除某些文件或文件夹,但是您想要删除它们内部的所有内容。

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

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

还有很多其他解决方案,但我发现这很容易理解和记住。

#7


0  

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
}

#8


0  

I used this, that works perfectly for me

我用过这个,对我来说很有效

Get-ChildItem -Path 'C:\Temp\*' -Recurse | Where-Object {($_.FullName -notlike "*windirstat*") -and ($_.FullName -notlike "C:\Temp\GetFolderSizePortable*")} | Remove-Item -Recurse

#9


0  

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

根据MSDN Remove-Item与-exclude参数有一个已知问题。请改用此变体。

Get-ChildItem * -exclude folderToExclude | Remove-Item