英文:
Delete Files under certain Folders and Leave Folders Intact with Powershell
问题
以下是翻译好的部分:
我正在尝试清理文件夹,保留文件夹,但删除文件。但我不想删除某些文件夹中的任何内容,但我无法弄清楚如何操作。到目前为止,我有以下代码,但它会删除每个文件夹中的所有内容,并且还会删除第一级上的两个脚本。我已经用红色矩形标记的那些我需要脚本忽略,其他文件夹我希望从中删除文件。
Get-ChildItem -Path 'C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders' -Exclude "*.exe","*.config","assets", "Documentation" -Recurse | ForEach-Object {$_.Delete()}
英文:
I am trying to clean up folders, keeping the folders, but removing the files. I do not want to delete anything in some folders though and cannot figure out how. Here is what I have so far, but it deletes everything in each folder and also removes the 2 scripts on the first level. The ones I have marked with a red rectangle I need the script to ignore, the other folders I want the files deleted from.
Get-ChildItem -Path 'C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders' -Exclude "*.exe","*.config","assets", "Documentation" -Recurse | ForEach-Object {$_.Delete()}
答案1
得分: 0
从Get-ChildItem
调用中移除-Recurse
,将结果传递给另一个Get-ChildItem
调用,以确保在目标文件夹中的项目中包括子目录的项目被列举(但不包括在输出中),并将结果传递给Remove-Item
:
Get-ChildItem `
-Path 'C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders' `
-Exclude *.exe, *.config, assets, Documentation |
Get-ChildItem -Force |
Remove-Item -Recurse -WhatIf
注意:
-
即使您的输入路径是字面路径,因此通常应通过
-LiteralPath
传递,但Windows PowerShell中的一个错误(在PowerShell(Core)中已修复)会导致在与-LiteralPath
结合使用时忽略-Include
和-Exclude
- 如果您使用
-Include
而不是-Exclude
,您还必须确保-Path
值以\*
结尾(或只是*
),原因请参见此答案。
- 如果您使用
-
请注意,通过第二个
Get-ChildItem
调用的枚举包括任何嵌套的子目录,这些子目录也会被Remove-Item
删除(-Recurse
开关会防止出现确认提示)。-
如果您只想从这些目录中删除文件,请在第二个
Get-ChildItem
调用中添加-File
。 -
如果您想递归地从这些目录中删除所有文件,同时保留嵌套的子目录,请添加
-File -Recurse
。
-
英文:
<!-- language-all: sh -->
Remove -Recurse
from the Get-ChildItem
call, pipe the results to another Get-ChildItem
call to ensure that subdirectories among the item in the target folders have their items enumerated (without being included in the output themselves), and pipe the results to Remove-Item
:
Get-ChildItem `
-Path 'C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders' `
-Exclude *.exe, *.config, assets, Documentation |
Get-ChildItem -Force |
Remove-Item -Recurse -WhatIf
<sup>Note: The -WhatIf
common parameter in the command above previews the operation. Remove -WhatIf
and re-execute once you're sure the operation will do what you want.</sup>
Note:
-
Even though your input path is a literal path and should therefore normally be passed via
-LiteralPath
, a bug in Windows PowerShell (since fixed in PowerShell (Core)) causes-Include
and-Exclude
to be ignored when combined with-LiteralPath
- If you were to use
-Include
rather than-Exclude
you'd have to additionally ensure that your-Path
value ends in\*
(or is just*
), for the reasons explained in this answer.
- If you were to use
-
Note that the enumeration via the second
Get-ChildItem
call includes any nested subdirectories, which are also removed byRemove-Item
(the-Recurse
switch prevents a confirmation prompt).-
If you only want to remove the files from these directories, add
-File
to the secondGet-ChildItem
call. -
If you want to remove all files from these directories recursively while retaining nested subdirectories, add
-File -Recurse
.
-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论