Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On Windows, I need to find all files in a directory that contain UTF-8 BOM (byte-order mark). Which tool can do that and how?

It can be a PowerShell script, some text editor's advanced search feature or whatever.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Here is an example of a PowerShell script. It looks in the C: path for any files where the first 3 bytes are 0xEF, 0xBB, 0xBF.

Function ContainsBOM
{   
    return $input | where {
        $contents = [System.IO.File]::ReadAllBytes($_.FullName)
        $_.Length -gt 2 -and $contents[0] -eq 0xEF -and $contents[1] -eq 0xBB -and $contents[2] -eq 0xBF }
}

get-childitem "C:\*.*" | where {!$_.PsIsContainer } | ContainsBOM

Is it necessary to "ReadAllBytes"? Maybe reading just a few first bytes would perform better?

Fair point. Here is an updated version that only reads the first 3 bytes.

Function ContainsBOM
{   
    return $input | where {
        $contents = new-object byte[] 3
        $stream = [System.IO.File]::OpenRead($_.FullName)
        $stream.Read($contents, 0, 3) | Out-Null
        $stream.Close()
        $contents[0] -eq 0xEF -and $contents[1] -eq 0xBB -and $contents[2] -eq 0xBF }
}

get-childitem "C:\*.*" | where {!$_.PsIsContainer -and $_.Length -gt 2 } | ContainsBOM
share|improve this answer
    
Cool. Before I mark is as the answer, is it necessary to "ReadAllBytes"? Maybe reading just a few first bytes would perform better? –  Borek Apr 30 '12 at 1:27
    
@Borek See edit. –  vcsjones Apr 30 '12 at 2:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.