[英]Using Powershell to recursively search directory for files that only contain zeros
I have a directory that contains millions of files in binary format. 我有一个目录,其中包含数百万个二进制格式的文件。 Some of these files were written to the disk wrong (no idea how).
其中一些文件被错误地写入磁盘(不知道如何)。 The files are not empty, but they only contain zeros.
文件不是空的,但它们仅包含零。 Heres an example http://pastebin.com/5b7jHjgr
这是一个例子http://pastebin.com/5b7jHjgr
I need to search this directory, find the files that are all zeros and write their path out to a file. 我需要搜索该目录,找到全为零的文件,并将其路径写出到文件中。
I've been experimenting with format-hex and get-content, but my limited powershell experience is tripping me up. 我一直在尝试使用format-hex和get-content,但是我有限的powershell经验使我大跌眼镜。 Format-Hex reads the entire file, when I only need the first few bytes, and Get-Content expects text files.
当我只需要前几个字节时,Format-Hex会读取整个文件,而Get-Content需要文本文件。
Use IO.BinaryReader : 使用IO.BinaryReader :
Get-ChildItem r:\1\ -Recurse -File | Where {
$bin = [IO.BinaryReader][IO.File]::OpenRead($_.FullName)
foreach ($byte in $bin.ReadBytes(16)) {
if ($byte) { $bin.Close(); return $false }
}
$bin.Close()
$true
}
In the old PowerShell 2.0 instead of -File
parameter you'll need to filter it manually: 在旧的PowerShell 2.0中,您需要手动过滤它,而不是
-File
参数:
Get-ChildItem r:\1\ -Recurse | Where { $_ -is [IO.FileInfo] } | Where { ..... }
You can use a System.IO.FileStream
object to read the first n
bytes of each file. 您可以使用
System.IO.FileStream
对象读取每个文件的前n
个字节。
The following code reads the first ten bytes of each file: 以下代码读取每个文件的前十个字节:
Get-ChildItem -Path C:\Temp -File -Recurse | ForEach-Object -Process {
# Open file for reading
$file = [System.IO.FileStream]([System.IO.File]::OpenRead($_.FullName))
# Go through the first ten bytes of the file
$containsTenZeros = $true
for( $i = 0; $i -lt $file.Length -and $i -lt 10; $i++ )
{
if( $file.ReadByte() -ne 0 )
{
$containsTenZeros = $false
}
}
# If the file contains ten zeros then add its full path to List.txt
if( $containsTenZeros )
{
Add-Content -Path List.txt -Value $_.FullName
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.