Private/Test-IsAscii.ps1
function Test-IsAscii([System.IO.FileInfo]$item) { begin { $validList = New-Object Collections.Generic.List[Byte] $validList.AddRange([byte[]] (10,13) ) $validList.AddRange([byte[]] (31..127) ) } process { try { $reader = $item.Open([System.IO.FileMode]::Open) if (!$reader) { return $false } $bytes = new-object byte[] 1024 $numRead = $reader.Read($bytes, 0, $bytes.Count) for($i=0; $i -lt $numRead; ++$i) { if (!$validList.Contains($bytes[$i])) { return $false } } return $true } finally { if ($reader) { $reader.Dispose() } } }} |