51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

删除特定文件夹下的文件并保留文件夹完好无损,使用PowerShell。

英文:

Delete Files under certain Folders and Leave Folders Intact with Powershell

问题 {#heading}

以下是翻译好的部分:

我正在尝试清理文件夹,保留文件夹,但删除文件。但我不想删除某些文件夹中的任何内容,但我无法弄清楚如何操作。到目前为止,我有以下代码,但它会删除每个文件夹中的所有内容,并且还会删除第一级上的两个脚本。我已经用红色矩形标记的那些我需要脚本忽略,其他文件夹我希望从中删除文件。

Get-ChildItem -Path 'C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders' -Exclude "*.exe","*.config","assets", "Documentation" -Recurse | ForEach-Object {$_.Delete()}

删除特定文件夹下的文件并保留文件夹完好无损,使用PowerShell。 英文:

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()}

删除特定文件夹下的文件并保留文件夹完好无损,使用PowerShell。

答案1 {#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 &#39;C:\Users\Tara.verity\Documents_Local\RemoveFilesFromFolders&#39; `
    -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.
  • Note that the enumeration via the second Get-ChildItem call includes any nested subdirectories, which are also removed by Remove-Item(the -Recurse switch prevents a confirmation prompt).

    • If you only want to remove the files from these directories, add -File to the second Get-ChildItem call.

    • If you want to remove all files from these directories recursively while retaining nested subdirectories , add -File -Recurse.


赞(1)
未经允许不得转载:工具盒子 » 删除特定文件夹下的文件并保留文件夹完好无损,使用PowerShell。