
[英]Recursively copy contents of directory and subdirectories in powershell
[英]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.