简体   繁体   English

使用Powershell递归搜索目录以查找仅包含零的文件

[英]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-h​​ex和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.

相关问题 如何使用 PowerShell 递归搜索目录和子目录中的所有文件? - How to recursively search all files in a directory and sub-directories using PowerShell? 如何使用PowerShell递归搜索目录中的所有文件,包括隐藏目录中的隐藏文件? - How to recursively search a directory for all files including hidden files in hidden directories, with PowerShell? 使用Powershell仅复制文件而不复制目录结构 - copy files only not directory structure using powershell 在目录中递归列出不需要的文件 - Powershell - Listing unwanted files recursively in a directory - Powershell Powershell以递归方式获取目录中文件的过滤列表 - Powershell Recursively getting filtered list of files in directory Powershell 脚本,用于验证目录中的文件是否包含密码 - Powershell script to verify if files in a directory contain passwords 在目录上以递归方式使用PowerShell运行简单命令 - Run a simple command using PowerShell recursively on a directory 使用Powershell递归处理目录统计信息 - Process directory statistics recursively using Powershell 在目录中搜索模式并使用PowerShell从文件中提取字符串 - Search pattern in directory and extract string from files using PowerShell 使用 powershell 在目录中的所有 the.log 文件中搜索字符串 - search a string in all the .log files in a directory using powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM