user1682654
user1682654

Reputation: 55

Would like to write bytes to multiple binary files

I need to write the same bytes to all files with extension TIFF in a particular folder. I am completely new to PowerShell (or any other coding for that matter). I have worked out how to change the bytes in a single file like this (simplified for this post):

$bytes  = [System.IO.File]::ReadAllBytes("C:\test.tiff")
$bytes[5] = 0x51
$bytes[8] = 0x4A
[System.IO.File]::WriteAllBytes("C:\test.tiff", $bytes)

However, rather than specifying the file I need to loop through the files in a particular folder to check if they have the extension TIFF before writing the bytes and I am not sure how to do that. Any help would be appreciated.

Upvotes: 3

Views: 327

Answers (1)

colsw
colsw

Reputation: 3326

use a ForEach loop with Get-ChildItem

foreach ($File in (Get-ChildItem 'C:\Folder' -Filter '*.tiff')){
    $bytes  = [System.IO.File]::ReadAllBytes($File.FullName)
    $bytes[5] = 0x51 ; $bytes[8] = 0x4A
    [System.IO.File]::WriteAllBytes($File.FullName, $bytes)
}

change C:\Folder to your folder, this will modify only .tiff files.

Upvotes: 3

Related Questions