Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. # 固定長のShift_JISファイルの特定位置の文字を削除する
  2. $filename = "hogehoge.txt"
  3. $tempfile = "ZZZZZ_$filename"
  4. $enc = [Text.Encoding]::GetEncoding("Shift_JIS")
  5. $fhIn = New-Object System.IO.StreamReader($filename, $enc)
  6. $fhOut = New-Object System.IO.StreamWriter($tempfile, $false, $enc)
  7. $isCuted = $FALSE
  8.  
  9. # 1行ずつ処理
  10. while (($l = $fhIn.ReadLine()) -ne $null) {
  11. # 1文字ずつ処理
  12. $tmpString = [System.Linq.Enumerable]::ToArray($l)
  13.  
  14. if($enc.GetByteCount($l) -le 760) {
  15. Write-Host "すでに加工済みです"
  16. $isCuted = $TRUE
  17. break
  18. }
  19.  
  20. $iCount = 0
  21. $sOutLine = ""
  22. foreach($tmpChar in $tmpString) {
  23. $iCount = $iCount + $enc.GetByteCount($tmpChar)
  24.  
  25. # 特定位置の文字を処理しない
  26. switch($iCount) {
  27. {$_ -le 550} {$sOutLine = $sOutLine + $tmpChar} # <= 549
  28. {$_ -ge 552 -band $_ -le 744} {$sOutLine = $sOutLine + $tmpChar} # >= 552 AND <= 744
  29. }
  30. }
  31.  
  32. $fhOut.WriteLine($sOutLine)
  33. }
  34.  
  35. $fhIn.Close()
  36. $fhOut.Close()
  37.  
  38.  
  39. # ファイル名の変更
  40. if($isCuted -eq $TRUE) {
  41. Remove-Item $tempfile
  42. } else {
  43. $nowDate = Get-Date -Format "yyyyMMdd_HHmmss"
  44. Move-Item $filename "$nowDate-$filename"
  45. Move-Item $tempfile $filename
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement