想将字节写入多个二进制文件

时间:2017-11-15 11:44:31

标签: powershell loops file-io

我需要将相同的字节写入特定文件夹中具有扩展名TIFF的所有文件。我对PowerShell(或任何其他编码)完全不熟悉。我已经研究了如何在这样的单个文件中更改字节(本文简化):

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

但是,我不是指定文件,而是需要循环浏览特定文件夹中的文件,以便在写入字节之前检查它们是否具有扩展名TIFF,我不知道该怎么做。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:2)

使用ForEach循环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)
}

C:\Folder更改为您的文件夹,这只会修改.tiff个文件。