繁体   English   中英

如何使用Powershell递归地覆盖目录及其子目录中的每个文件(0字节/无)?

[英]How to use Powershell to recursively overwrite every file in a directory and its subdirectories with 0 bytes/nothing?

我有一个包含许多子目录和各种文件(带有或不带有扩展名)的目录。 我希望能够保留文件夹结构以及所有文件的名称,但要删除实际数据,同时减小目录的大小。 也许它甚至只能覆盖一定大小的文件,但这不是必需的。 我认为使用Powershell可能可行,但我愿意接受其他选择。 非常感谢您提供的所有帮助!

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

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

请注意-NoNewLine仅在PowerShell的较新版本中可用。

关于将文件归零的速度, Clear-Content是最快的方法,在10,000次迭代中运行时间为12.9秒。

  • New-Item -Force运行时间为13.1秒。
  • $null | Out-File $null | Out-File运行于15.2秒。
  • '' | Out-File '' | 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 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM