1

I have binary files larger than 50 GB, which contain a specific string I want to replace with equal length all-spaces string. The string I am looking for is in the beginning of file, say within the first megabyte. How can I do this with PowerShell?

I am afraid [System.IO.File]::ReadAllBytes("myfile.bin") is not the solution, because I don't want to load the whole binary. I want to search and replace within the first megabyte.

1 Answer 1

6

Adopted from C#, so some refactoring might be needed:

$path = "\path\to\binary\file"

$numberOfBytesToRead = 1000000

$stringToSearch = "Hello World!"
$enc = [system.Text.Encoding]::UTF8
[Byte[]]$replacementString = $enc.GetBytes("     ");

$fileStream = [System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)

# binary reader to search for the string 
$binaryReader = New-Object System.IO.BinaryReader($fileStream)

# get the contents of the beginning of the file
[Byte[]] $byteArray = $binaryReader.ReadBytes($numberOfBytesToRead)

# look for string
$m = [Regex]::Match([Text.Encoding]::ASCII.GetString($byteArray), $stringToSearch)
if ($m.Success)
{    
    echo "Found '$stringToSearch' at position "$m.Index
}
else
{
    echo "'$stringToSearch' was not found"
}
$fileStream.Close()

# reopen to write
$fileStream = [System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite)

$binaryWriter = New-Object System.IO.BinaryWriter($fileStream)

# set file position to location of the string
$binaryWriter.BaseStream.Position = $m.Index; 
$binaryWriter.Write($replacementString)

$fileStream.Close()
Sign up to request clarification or add additional context in comments.

2 Comments

This needs an explicit check for byte representation of $stringToSearch to be equal to that of $replacementString. Otherwise, looks fine.
You can use $index = [Text.Encoding]::ASCII.GetString($byteArray).IndexOf($stringToSearch) instead of Regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.