如何使用Powershell以0字节/无任何方式递归覆盖目录及其子目录中的每个文件?

问题描述 投票:0回答:2

我有一个目录,包含许多子目录和各种文件(有和没有扩展名)。我希望能够保留文件夹结构以及所有文件的名称,但删除实际数据,保持目录的大小。也许它甚至只能覆盖一定大小的文件,但这不是必需的。我认为它可能与Powershell有关,但我对替代方案持开放态度。非常感谢任何和所有的帮助!

powershell recursion directory-structure
2个回答
2
投票

使用Get-ChildItem检索文件列表。使用Out-File来写每个。

Get-ChildItem C:\example\Path -Recurse -File | 
    ForEach-Object {
        "" | Out-File -Path $_.FullName -NoNewLine -Force
    }

请注意,-NoNewLine仅适用于较新版本的PowerShell。


1
投票

关于速度为零的文件,Clear-Content12.9迭代时10,000秒运行时间最快的方法。

  • New-Item -Force跑了13.1秒。
  • $null | Out-File跑了15.2秒。
  • '' | Out-File跑了18.2秒。
  • Set-Content $null跑了20.7秒。
  • Set-Content ''跑了25.8秒。
  • $null > file跑了16.7秒。
  • '' > file跑了18.1秒。

功能示例:

function Write-Zero([string] $Path) { Clear-Content -Path $Path -Force }
© www.soinside.com 2019 - 2024. All rights reserved.