vbsにつづき、今度はpowershellで
初歩的なひな型を作成した。
powershellは一度にすべてのファイルを読むこともできるが、
編集とかを考えた場合、やはり、1行ずつ読んで、1行ずつ
書くほうが処理も速いし、メモリも食わない。
(勝手な憶測だが)
まず、powershellは以下のコードだ。拡張子を.ps1にする。
編集はサクラエディタでタイプ別設定一覧のperlに拡張子,ps1を追加すると
編集がしやすい。
# Microsoft.VisualBasic.Interaction
using namespace Microsoft.VisualBasic
Set-PSDebug -Strict
Add-Type -Assembly Microsoft.VisualBasic
# ファイル名入力
$inp= [Interaction]::InputBox("ファイル名を入力してください。", "入力")
$enc = [System.Text.Encoding]::GetEncoding(932)
# 入力ファイルオープン
$reader = New-Object System.IO.StreamReader($inp, $enc,$false)
# 出力ファイルオープン
$writer = New-Object System.IO.StreamWriter("$($inp)$("_out.txt")",$false , $enc)
# 入力ファイルから一行ずつ読み込み
while(!($reader.EndOfStream)) {
# 一行読み込み
$read_data = $reader.ReadLine()
# 条件判断
if ($read_data.Substring(1,1) -eq "1") {
# レコードを1件書き込み
$writer.WriteLine($read_data)
}
}
$reader.Close()
$writer.Close()
# 終了メッセージ
[System.Windows.Forms.MessageBox]::Show("処理は終了しました")
次に、動かすのは、.batで行う。こちらは、拡張子.batに変え、こちらを実行する。
@echo off
powershell -executionpolicy RemoteSigned -File "sample.ps1"