Trying to delete all files in all subfolders with a Powershell script, but can't find a way to exclude the deletion of files in the root folder

huangapple go评论68阅读模式
英文:

Trying to delete all files in all subfolders with a Powershell script, but can't find a way to exclude the deletion of files in the root folder

问题

我写了一个PowerShell脚本,用于删除当前路径的子文件夹中的所有文件。它能正常工作,但也会删除当前/根文件夹中的所有文件。如何排除该文件夹?

$CountedFiles = (Get-ChildItem -File -Recurse | Measure-Object).Count
$NewCountedFiles = ($CountedFiles - 1)
$path = Get-Location
Get-ChildItem -Path $path -Include *.* -Exclude *.ps1 -File -Recurse | foreach { $_.Delete()}

Write-Host "已删除 $NewCountedFiles 个文件,包括所有子文件夹和当前文件夹,路径为: $path" 

pause

目前,我已经排除了.ps1(PowerShell脚本文件)以进行测试,以防它删除自身。

我尝试找到一种排除根文件夹的方法,但由于我是PowerShell的新手,还没有找到。希望有人知道一个简单的方法来实现。

英文:

I wrote a Powershell script that deletes all files in the subfolders of the current path.
It works fine, but it also deletes all files in the current/root folder. How can I exclude that folder?

$CountedFiles = (Get-ChildItem -File -Recurse | Measure-Object).Count
$NewCountedFiles = ($CountedFiles - 1)
$path = Get-Location
Get-ChildItem -Path $path -Include *.* -Exclude *.ps1 -File -Recurse | foreach { $_.Delete()}

Write-Host "Es wurde(n) '$NewCountedFiles' Datei(en) in allen Unterordnern und im derzeitigen Ordner im Pfad: '$path' gelöscht!" 

pause

Currently I have it so that it exludes the .ps1 (Powershell Script File) for testing so it doesn't deletes itself.

I tried to find a way to exclude the root folder, but haven't found one since I am new to Powershell. I hope that someone knows an easy why to do so.

答案1

得分: 0

如果您需要删除文件,但不删除子文件夹,您可以尝试以下操作:

$files = Get-ChildItem -Path $pwd -File -Recurse
$filesToDelete = $files | Where-Object { $_.Directory.FullName -ne $pwd }
$filesToDelete | Remove-Item -Force

这样可以删除所有文件,而不会删除根目录内容和子文件夹。

英文:

If you need to delete the files, but not the subfolders, you can try this:

$files = Get-ChildItem -Path $pwd -File -Recurse
$filesToDelete = $files | Where-Object { $_.Directory.FullName -ne $pwd }
$filesToDelete | Remove-Item -Force

And you delete all the files without remove the root content and the subfolders.

答案2

得分: 0

你可以只迭代当前位置的顶级子目录子项,而不是从当前位置本身开始:

Get-ChildItem -Directory | ForEach { 
  Get-ChildItem -Recurse $_ | Sort-Object -Property FullName | ForEach {
    $_.Delete() 
  }
}

这将删除子目录和文件;如果你只想删除文件,请使用Victor Silva的答案。

英文:

You could just iterate over the top-level subdirectory children of the current location instead of starting at the current location itself:

Get-ChildItem -Directory | ForEach { 
  Get-ChildItem -Recurse $_ | Sort-Object -Property FullName | ForEach {
    $_.Delete() 
  }
}

That will delete subdirectories as well as files; if you only want to delete the files, go with Victor Silva's answer.

答案3

得分: 0

以下是您要翻译的内容:

# 以下是 PowerShell 习惯用法的解决方案,利用了 [`Get-ChildItem`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Get-ChildItem) 和 [`Remove-Item`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Remove-Item) 中的直接管道支持:

Get-ChildItem $path -Directory |  # 仅获取子目录
  Get-ChildItem -Include *.* -Exclude *.ps1 -File -Recurse | # 在每个目录中查找文件
  Remove-Item -WhatIf

# 注意:上述命令中的 [`-WhatIf` 通用参数](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters#-whatif) 用于 _预览_ 操作。在确保操作会执行所需操作后,删除 `-WhatIf` 并重新执行命令。

# 如果在第二个 `Get-ChildItem` 调用中添加 [`-OutVariable`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_CommonParameters#-outvariable)` files`,您可以使用 `$files.Count` 来获取稍后删除的文件数。
英文:

<!-- language-all: sh -->

A PowerShell-idiomatic solution that takes advantage of direct pipeline support in Get-ChildItem and Remove-Item:

Get-ChildItem $path -Directory |  # Get only the subdirectires
  Get-ChildItem -Include *.* -Exclude *.ps1 -File -Recurse | # Look for files in each
  Remove-Item -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>

If you add -OutVariable files to the 2nd Get-ChildItem call, you can use $files.Count to get the count of deleted files later.

huangapple
  • 本文由 发表于 2023年5月25日 19:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定