<変形元>
固定長SJISテキストデータにCrLfを付加(VBScriptにて試行2 自給自足版)
http://blog.goo.ne.jp/tkhs1732/e/b73e022f14f014e468ea60cb0a859cae
<FixedToTSV.vbs>SJIS固定長区切るツール
Option Explicit
Dim byteDat(255)
Call Make256
Dim oExcel 'As Object
Dim FileRead 'As String
Dim FileWrite 'As String
Dim strPathIN 'As String
Dim strPathOUT 'As String
Dim oADOST_R 'As Object
Dim oADOST_W 'As Object
Dim i 'As Long
Dim readPos 'As Long ' or Currency or Double
Dim SJIS_NUM 'As Integer
Dim dthead1
Dim dthead2
Dim KBN
'************レングスをセットしてください。 ****************
SJIS_NUM = 150 + 2
'***********************************************************
'ReDim SJIS_buf(SJIS_NUM - 1 + 2) '2=Crlf
Set oExcel = CreateObject("Excel.Application")
FileRead = oExcel.GetOpenFilename("インプット(*.*),*")
FileWrite = oExcel.GetSaveAsFilename( _
"FixedToTSV_Out.txt" _
, "テキストファイル(*.txt),*.txt" & _
",その他のファイル(*.*),*.*" _
, 1 _
, "保存先の指定" _
)
oExcel.Quit
Set oExcel = Nothing
If UCase(FileRead) = "FALSE" Then
MsgBox "入力ファイルの選択がキャンセルされました。" & vbLf & _
"処理を終了します。"
'Exit Sub
WScript.Quit
End If
If UCase(FileWrite) = "FALSE" Then
MsgBox "出力ファイルの選択がキャンセルされました。" & vbLf & _
"処理を終了します。"
'Exit Sub
WScript.Quit
End If
strPathIN = FileRead
strPathOUT = FileWrite
'読込Object設定
Set oADOST_R = CreateObject("ADODB.Stream")
oADOST_R.Type = 1 '1=adTypeBinary 2=adTypeText
oADOST_R.Open
oADOST_R.LoadFromFile strPathIN
readPos = 0
oADOST_R.Position = readPos '読込開始位置
'dat_CrLf = oADOST_R.Read(2)
'書込Object設定
Set oADOST_W = CreateObject("ADODB.Stream")
oADOST_W.Type = 1 '1=adTypeBinary 2=adTypeText
'oADOST_W.Charset = "iso-8859-1" 'キャラクタセット=Latin-1
oADOST_W.Open
'readPos = 2 'CrLf分進める
'oADOST_R.Position = readPos '読込開始位置
Do Until oADOST_R.EOS = True
'dthead1 = UCase(Right("0" & Hex(AscB(oADOST_R.Read(1))) , 2 ))
'dthead2 = UCase(Right("0" & Hex(AscB(oADOST_R.Read(1))) , 2 ))
dthead1 = Chr(AscB(oADOST_R.Read(1)))
dthead2 = Chr(AscB(oADOST_R.Read(1)))
KBN = dthead1 & dthead2
oADOST_R.Position = readPos
If KBN = "00" Then
Call KBN00
ElseIf KBN = "01" Then
Call KBN01
ElseIf KBN = "02" Then
Call KBN02
ElseIf KBN = "03" Then
Call KBN03
ElseIf KBN = "99" Then
Call KBN99
End If
oADOST_W.Write byteDat(&H0D)
oADOST_W.Write byteDat(&H0A)
readPos = readPos + SJIS_NUM
oADOST_R.Position = readPos
Loop
'既にファイルが存在する場合 1=実行時エラー、2=上書保存
oADOST_W.SaveToFile strPathOUT, 2
oADOST_R.Close
oADOST_W.Close
Set oADOST_R = Nothing
Set oADOST_W = Nothing
MsgBox "処理終了"
Function Make256()
Dim oADOST_W256 'As Object
Dim oADOST_R256 'As Object
Dim i 'As Long
Dim readPos
Dim oFS 'As Object
'書込Object設定
Set oADOST_W256 = CreateObject("ADODB.Stream")
oADOST_W256.Type = 2 'adTypeText
oADOST_W256.Charset = "iso-8859-1" 'キャラクタセット=Latin-1
oADOST_W256.Open
For i = 0 To 255
oADOST_W256.WriteText ChrW(i)
Next
'既にファイルが存在する場合 1=実行時エラー、2=上書保存
oADOST_W256.SaveToFile "byte256.tmp", 2
oADOST_W256.Close
Set oADOST_W256 = Nothing
'読込Object設定
Set oADOST_R256 = CreateObject("ADODB.Stream")
oADOST_R256.Type = 1 '1=adTypeBinary 2=adTypeText
oADOST_R256.Open
oADOST_R256.LoadFromFile "byte256.tmp"
For readPos = 0 To 255
oADOST_R256.Position = readPos '読込開始位置
byteDat(readPos) = oADOST_R256.Read(1)
Next
oADOST_R256.Close
Set oADOST_R256 = Nothing
Set oFS = CreateObject("Scripting.FileSystemObject")
oFS.DeleteFile("byte256.tmp")
Set oFS = Nothing
End Function
Function KBN00()
oADOST_W.Write oADOST_R.Read(002) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(006) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(142)
End Function
Function KBN01()
oADOST_W.Write oADOST_R.Read(002) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(015) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(060) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(073)
End Function
Function KBN02()
oADOST_W.Write oADOST_R.Read(002) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(015) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(010) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(008) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(115)
End Function
Function KBN03()
oADOST_W.Write oADOST_R.Read(002) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(015) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(060) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(024) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(001) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(048)
End Function
Function KBN99()
oADOST_W.Write oADOST_R.Read(002) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(009) : oADOST_W.Write byteDat(&H09)
oADOST_W.Write oADOST_R.Read(139)
End Function
<20131104追記始>
①FixedToTabしたファイルを開いて、
Ctrl + a 、 Ctrl + c
② Excel 開いて 貼り付け
固定長の種VBScript
下記scriptを実行し、
適当に半角2文字を全角1文字に置き換えて試す。項目境界#はだめ。
<Fixed_seed.vbs>
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim TargetFile 'As String
Dim oFS 'As Object
Dim oFR 'As Object
Dim oFW 'As Object
Dim i 'As Long
Dim out_area(4)
Dim KBN
Call Make_out
TargetFile = "FIXED_SEED.txt"
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFW = oFS.OpenTextFile(TargetFile, ForWriting, True)
For i = 1 To 400
KBN = Int(Rnd()*5)
'CrLf付き
oFw.Writeline out_Area(KBN)
'Crlf無し
'oFw.Write out_Area(KBN)
Next
oFW.Close
Set oFW = Nothing
Set oFS = Nothing
MsgBox "処理終了"
Function Make_out()
out_area(0) = "00" & _
String(005,"A") & "#" & _
String(141,"B") & "#"
out_area(1) = "01" & _
String(014,"C") & "#" & _
String(059,"D") & "#" & _
String(072,"E") & "#"
out_area(2) = "02" & _
String(014,"F") & "#" & _
String(009,"G") & "#" & _
String(007,"H") & "#" & _
String(114,"J") & "#"
out_area(3) = "03" & _
String(014,"K") & "#" & _
String(059,"L") & "#" & _
String(023,"M") & "#" & _
String(001,"N") & _
String(047,"P") & "#"
out_area(4) = "99" & _
String(008,"Q") & "#" & _
String(138,"R") & "#"
End Function
<20131104追記終>