目次

ファイル調査3:データを作る

2025-09-12
自分用メモ

これは何?

PowerShellでバイト列をファイルに書き出す手順の簡単な例。

ファイル調査2:バイト配列から文字列に変換の続きで、テスト用に1レコード分の新規データファイルを作ってみる。

バイト配列にデータ格納

項目TAGが99のテストデータが必要になったとしよう。

既に1レコード14バイトと分かっていて、レコード内項目の構成も分かっている。
項目IDには0004、項目NAMEにはSJISで“秋田犬 ”を格納したファイル SAMPLEBIN2 を作成にしよう。

バイト配列に項目ID、項目NAME、項目TAG、の順に各バイト列を格納していけばいい。

項目NAMEの生成

項目NAMEに格納するSJISの文字列のバイト列変換結果を得るには以下のようなEncoding.GetBytes()を利用すればいい。

PS E:\WK> [System.Text.Encoding]::GetEncoding(932).GetBytes("秋田犬 ") | Format-Hex

   Label: Byte (System.Byte) <0C515C73>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 8F 48 93 63 8C A2 81 40                         �H�c�¢�@

PS E:\WK>

ファイル書き出し

PowerShellのバージョンでオプションが異なるので注意。

ここの例では新規作成のため、Set-Content コマンドレットを使っている。

PowerShell 7.5 の場合

Set-Content のオプションが 5.1 のものとは違うので注意。

PS E:\WK> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.5.2
PSEdition                      Core
GitCommitId                    7.5.2
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS E:\WK> $enc = [System.Text.Encoding]::GetEncoding(932)
PS E:\WK> [byte[]]$recdata = (0xF0, 0xF0,0xF4, 0xF0) + $enc.GetBytes("秋田犬 ") + ( 0xF9, 0xF9 )
PS E:\WK> $recdata | Set-Content -Path SAMPLEBIN2 -AsByteStream
PS E:\WK> Format-Hex SAMPLEBIN2

   Label: E:\WK\SAMPLEBIN2

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 F0 F0 F4 F0 8F 48 93 63 8C A2 81 40 F9 F9       ððôð�H�c�¢�@ùù  ← 想定通りのデータになった

PS E:\WK>

PowerShell 5.1 の場合

Get-Content のオプションが 7.5 のものとは違うので注意。

PS E:\WK> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.5697
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.5697
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS E:\WK> $enc = [System.Text.Encoding]::GetEncoding(932)
PS E:\WK> [byte[]]$recdata = (0xF0, 0xF0,0xF4, 0xF0) + $enc.GetBytes("秋田犬 ") + ( 0xF9, 0xF9 )
PS E:\WK> $recdata | Set-Content -Path SAMPLEBIN2 -Encoding Byte
PS E:\WK> Format-Hex SAMPLEBIN2


           パス: C:\Users\metan\SAMPLEBIN2

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   F0 F0 F4 F0 8F 48 93 63 8C A2 81 40 F9 F9        ððôðHc¢@ùù ← 想定通りのデータになった


PS E:\WK>

追記:2レコード作りたい

おいおい応用力が足りんぞー

PowerShell 7.5 の例を提示する。

PS E:\WK> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.5.2
PSEdition                      Core
GitCommitId                    7.5.2
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS E:\WK> $enc = [System.Text.Encoding]::GetEncoding(932)
PS E:\WK> [byte[]]$recdata = (0xF0, 0xF0,0xF4, 0xF0) + $enc.GetBytes("秋田犬 ") + ( 0xF9, 0xF9 )  ← 1レコード目のバイトデータ格納
PS E:\WK> $recdata += (0xF0, 0xF0,0xF5, 0xF0) + $enc.GetBytes("ボーダー") + ( 0xF9, 0xF9 )         ← 続けて2レコード目のバイトデータ格納
PS E:\WK> $recdata | Set-Content -Path SAMPLEBIN2 -AsByteStream                                   ← ファイルSAMPLEBIN2 へ書き出す
PS E:\WK> Format-Hex SAMPLEBIN2 -Offset 0 -Count 14  ← ファイル1レコード目

   Label: E:\WK\SAMPLEBIN2

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 F0 F0 F4 F0 8F 48 93 63 8C A2 81 40 F9 F9       ððôð�H�c�¢�@ùù

PS E:\WK> Format-Hex SAMPLEBIN2 -Offset 14 -Count 14  ← ファイル2レコード目

   Label: E:\WK\SAMPLEBIN2

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
000000000000000E F0 F0 F5 F0 83 7B 81 5B 83 5F 81 5B F9 F9       ððõð�{�[�_�[ùù

PS E:\WK> [byte[]]$bindata = Get-Content SAMPLEBIN2 -AsByteStream  ← ファイルSAMPLEBIN2を改めてメモリにバイト配列として確保する
PS E:\WK> $enc.GetString($bindata[4..11])  ← 1レコード目の項目NAME
秋田犬 
PS E:\WK> $enc.GetString($bindata[18..25])  ← 2レコード目の項目NAME
ボーダー
PS E:\WK>