汎用機メモっとくか

しごと用の(学習メモ&お気に入り保存)。

JScript でInputBoxを使う(Excel有りの環境下にて)

2019年11月23日 08時44分52秒 | WSH

<sample01.js>
var oExcel = new ActiveXObject("Excel.Application");
var target_file = oExcel.GetOpenFilename("読み込み (*.*),*");
var a01 = oExcel.InputBox("prompt","Title","Default");
oExcel.Quit();
delete oExcel;
WScript.Echo(target_file);
WScript.Echo(a01);

 


その他のやり方1
<sample02.bat>.BATにして動かす
@if(0)==(0) ECHO OFF
C:\Windows\SysWOW64\WScript.exe //NoLogo //E:JScript "%~f0" %1
GOTO :EOF
@end
var nVBS = new ActiveXObject("ScriptControl");
    nVBS.Language = "VBScript";
    nVBS.AddCode("Function func_msgbox(a01) " + "\n" +
                 " func_msgbox = MsgBox(a01)" + "\n" +
                 "End Function " + "\n" +
        "Function func_inputbox(prompt,title,default) " + "\n" +
        " func_inputbox = inputBox(prompt,title,default)" + "\n" +
        "End Function");
var oVBS = nVBS.CodeObject;
WScript.Echo(oVBS.func_msgbox("aaaaa"));
WScript.Echo(oVBS.func_inputbox("Prompt","Title","Default"));


64bit環境で VBA、VBScriptからJScript(javascript)の関数を使用する。

2019年02月04日 06時42分15秒 | EXCEL VBA

64bit環境で VBA、VBScriptからJScript(javascript)の関数を使用する。
”書いて忘れる”様
vba/vbscript:64bit環境でJSONをパース
http://bougyuusonnin.seesaa.net/article/446183415.html

”@nukie_53”様
Qiita VBAからJScriptのfunctionオブジェクトを使用する(64bit対応)
https://qiita.com/nukie_53/items/297e524bcc8e43f9b5d1

”Beginner'sMemo初心者備忘録”様
64ビット環境でのScriptControlの代わり
http://www.ka-net.org/office/of32.html

ありがとうございました。学習させていただきました。使わせていただきます。

上記3つの記事の使用方法を、学習後、もしやと思い、
吉岡さんのソースにあたってみた

googleで”吉岡照雄”で検索するとでてきます。
作者: 吉岡 照雄 - Vector
https://www.vector.co.jp/vpack/browse/person/an010222.html

Ctrl + Fで検索してください
●history.VBS
 「履歴」をHTMLファイルにする、「履歴」のサブフォルダを開くVBScript
<中身>
 History.TXT
 History.VBS
 History98.VBS
 TechNote.TXT
 最近使ったファイルメニュー.VBS
 履歴/今日/マイ コンピュータ.VBS

”History98.VBS” の中に CreateObject("htmlfile")の恐ろしいサンプルがあった。
2006-07-02に既に検討済みとは、本当に驚愕した。
<見所>
 (1) VBScript中でJavaScriptの new Array()を実現
  (2) VBScript中で(1)のArray の push 、shiftを行う
 (3) VBScriptから JScriptの定義関数を呼び出し、
   さらにそこからVBScriptの定義関数を使用させる

吉岡さんの”History98.VBS”から、抽出、改変しました。

Sub from_history98_vbs()
    Dim oHTML
    Set oHTML = CreateObject("htmlfile")
   
    oHTML.parentWindow.execScript _
    "function sortNum(data){var jsArr = data.split(/\t/)               ;" & _
    "                           jsArr.sort(compareNum)                 ;" & _
    "                           return jsArr.join(""\t"")              ;" & _
    "                      }                                           ;" & _
    "function compareNum(a,b){if(a * 1.0 > b * 1.0)   return     1     ;" & _
    "                         else                    return    -1     ;" & _
    "                      }                                           ;" & _
    "function sortStr(data){var jsArr = data.split(/\t/)               ;" & _
    "                           jsArr.sort(compareStr)                 ;" & _
    "                           return jsArr.join(""\t"")              ;" & _
    "                      }                                           ;" & _
    "function compareStr(a,b){if(a + """" > b + """") return     1     ;" & _
    "                         else                    return    -1     ;" & _
    "                      }                                           ;"
  
    Dim myData
    myData = _
    "999,777,888,9,7,8,99,77,88,333,111,222,3,1,2,33,11,22,666,444,555,6,4,5,66,44,55"
   
    Dim myArr
   
    Dim JSFunc
    Set JSFunc = oHTML.parentWindow
   
    myArr = JSFunc.sortNum(Join(Split(myData, ","), vbTab))
    MsgBox Join(Split(myArr, vbTab), vbLf)
   
     myArr = JSFunc.sortStr(Join(Split(myData, ","), vbTab))
    MsgBox Join(Split(myArr, vbTab), vbLf)
   
End Sub

<20190205追記STA>Excelでの負荷テストサンプル
Sub sheed_test01()
    Dim ix As Integer
    Dim iy As Integer
    Dim WS1 As Worksheet
    Set WS1 = Sheets("Sheet1")
   
    Dim WSFUNC As WorksheetFunction
    Set WSFUNC = Application.WorksheetFunction
   
    Application.ScreenUpdating = False
   
    For ix = 1 To 1000
        For iy = 1 To 12
            WS1.Cells(ix, iy).Value = WSFUNC.RandBetween(0, 30)
        Next
    Next
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub

 


Sub from_history98_vbs_B()
    Dim oHTML
    Set oHTML = CreateObject("htmlfile")
   
    oHTML.parentWindow.execScript _
    "function sortNum(data){var jsArr = data.split(/\t/)               ;" & _
    "                           jsArr.sort(compareNum)                 ;" & _
    "                           return jsArr.join(""\t"")              ;" & _
    "                      }                                           ;" & _
    "function compareNum(a,b){if(a * 1.0 > b * 1.0)   return     1     ;" & _
    "                         else                    return    -1     ;" & _
    "                      }                                           ;" & _
    "function sortStr(data){var jsArr = data.split(/\t/)               ;" & _
    "                           jsArr.sort(compareStr)                 ;" & _
    "                           return jsArr.join(""\t"")              ;" & _
    "                      }                                           ;" & _
    "function compareStr(a,b){if(a + """" > b + """") return     1     ;" & _
    "                         else                    return    -1     ;" & _
    "                      }                                           ;"
  
    Dim WS1 As Worksheet
    Dim WS2 As Worksheet
    Dim WS3 As Worksheet
   
    Set WS1 = Sheets("Sheet1")
    Set WS2 = Sheets("Sheet2")
    Set WS3 = Sheets("Sheet3")
   
    Dim JSFunc
    Set JSFunc = oHTML.parentWindow
   
    Dim WSFUNC As WorksheetFunction
    Set WSFUNC = Application.WorksheetFunction
   
    Dim ix As Integer
   
    Dim myData As Variant
    Dim myArr  As String
   
    Application.ScreenUpdating = False
   
    For ix = 1 To 1000
        myData = WSFUNC.Transpose(WSFUNC.Transpose(WS1.Range("A" & ix & ":L" & ix).Value))
        myArr = JSFunc.sortNum(Join(myData, vbTab))
        WS2.Range("A" & ix & ":L" & ix).Value = Split(myArr, vbTab)
    Next
    For ix = 1 To 1000
        myData = WSFUNC.Transpose(WSFUNC.Transpose(WS1.Range("A" & ix & ":L" & ix).Value))
        myArr = JSFunc.sortStr(Join(myData, vbTab))
        WS3.Range("A" & ix & ":L" & ix).Value = Split(myArr, vbTab)
    Next
    Application.ScreenUpdating = True
   
    MsgBox "Done"
End Sub
<20190205追記END>

 

 

<20190220追記STA>
'Sub JScrip_in_VBA4()
  Dim nJS
  Dim oJS
 
  Dim strPathIn
  Dim myRec
  Dim oADOST_R


    strPathIn = "C:\Users\user\Desktop\underscore-min.js"

    Set oADOST_R = CreateObject("ADODB.Stream")
    Dim adReadLine: adReadLine = -2
    oADOST_R.Type = 2 '-1--adTypeBinary , 2--adTypeText
    oADOST_R.Charset = "Utf-8"
    oADOST_R.LineSeparator = -1  ' -1 CrLf , 10 Lf , 13 Cr
    oADOST_R.Open
    oADOST_R.LoadFromFile = strPathIn
       
    myRec = ""
    Do While Not oADOST_R.EOS
        myRec = myRec & oADOST_R.ReadText(adReadLine) & vbCrLf
    Loop
    oADOST_R.Close
    Set oADOST_R = Nothing
  Dim oHTML
  Set oHTML = CreateObject("htmlfile")
 
  oHTML.ParentWindow.execscript myRec & vbCrLf & _
  "function shuffle(data){var jsArr  = data.split(/\t/)          ;" & _
  "                       var jsArr2 = _.shuffle(jsArr)          ;" & _
  "                       return jsArr2.join(""\t"")             ;" & _
  "                      }                                       ;" & _
  "function num_map(data){var jsArr  = data.split(/\t/)          ;" & _
  "                       var jsArr2 = _.map(jsArr, function(v){ ;" & _
  "                                          return v * 5;})     ;" & _
  "                       return jsArr2.join(""\t"")             ;" & _
  "                      }                                        "
 
  Dim JSFunc
  Set JSFUNC = oHTML.ParentWindow
 
  Dim mydata
  mydata = " 1, 2, 3, 4, 5, 6, 7, 8, 9,10"

  Dim res_data
  res_data  =   JSFUNC.shuffle(Join(Split(mydata, ","), vbTab))
  MsgBox Join(Split(res_data, vbTab), vbLf)

  res_data  =   JSFUNC.num_map(Join(Split(mydata, ","), vbTab))
  MsgBox Join(Split(res_data, vbTab), vbLf)

 'End Sub


 <20190220追記END>
 

 


でっきるかな?PowerShell 005 PowerShellでfoldコマンド

2018年12月07日 01時47分56秒 | PowerShell

環境 Windows10 64bit PowerShell version 5.0 にて動かす機会があった。
性能のいいマシンならそこそこ動くと思われる。

 Windows7 32bit PowerShell version 2.0 の低スペックノートPCでは、

メモリを大量消費の上、速度もコード量の割に遅い

[foldで150バイト]

[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_ + $crlf} | sc out_Fixed150.txt -encoding byte


[1レコードの先頭35バイトを取り出す]
[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_[0..34] + $crlf} | sc out_Fixed150.txt -encoding byte


参考元

プログラマブルPowerShell ~プログラマのための活用バイブル
~ (.NET TECHNOLOGYシリーズ)
単行本(ソフトカバー) ? 2008/1/8
荒井 省三 (著)

単行本(ソフトカバー): 400ページ
出版社: 技術評論社 (2008/1/8)
言語: 日本語
ISBN-10: 4774133329
ISBN-13: 978-4774133324
発売日: 2008/1/8

3-2-5 バイナリファイルを処理する P240
より

 

<20181208追記STA>PowerShell SJIS固定長 改行(CrLf)なし 分割サンプル
※ハイスペックマシンでないと帰ってこないかも

 

[int[]]$arr = @(2,15,10,8,115);[byte[]]$crlf = @(13,10);[byte[]]$tab = @(9);$MAX=($arr.length -1)
gc fixed150.txt -encoding byte -readcount 150 -totalcount (150*200) |%{
$buf = $_
$j = 0
for($i=0;$i -lt $arr.length;$i++){
    $jx = $j + ($arr[$i] - 1)
    Write-Output $buf[$j..$jx]
    if($i -ne $MAX){Write-Output $tab}
    $j = $j + $arr[$i]
}
Write-Output $crlf} | sc out_Fixed150bbb.txt -encoding byte

 

<2行にしたもの>
[int[]]$arr = @(2,15,10,8,115);[byte[]]$crlf = @(13,10);[byte[]]$tab = @(9);$MAX=($arr.length -1)
measure-command{gc fixed150.txt -encoding byte -readcount 150 -totalcount (150*200) |%{$buf=$_;$j=0;for($i=0;$i -lt $arr.length;$i++){$jx=$j+($arr[$i]-1);Write-Output $buf[$j..$jx];if($i -ne $MAX){Write-Output $tab};$j=$j+$arr[$i];}Write-Output $crlf} | sc out_Fixed150.txt -encoding byte}

 

 <結果>
PS C:\Users\user\desktop> measure-command{gc fixed150.txt -encoding byte -readcount 150 -totalcount (150*200) |%{$buf=$
_;$j=0;for($i=0;$i -lt $arr.length;$i++){$jx=$j+($arr[$i]-1);Write-Output $buf[$j..$jx];if($i -ne $MAX){Write-Output $t
ab};$j=$j+$arr[$i];}Write-Output $crlf} | sc out_Fixed150.txt -encoding byte}

Days : 0

Hours : 0
Minutes : 0
Seconds : 5
Milliseconds : 825
Ticks : 58255296
TotalDays : 6.74251111111111E-05
TotalHours : 0.00161820266666667
TotalMinutes : 0.09709216
TotalSeconds : 5.8255296
TotalMilliseconds : 5825.5296

PS C:\Users\user\desktop>

 

<20181208追記END>


<20181209追記STA>

文字列を特定の長さで改行する方法
https://social.technet.microsoft.com/Forums/ja-JP/d962931a-55cc-4bc5-9661-d554c78137ae/25991233832101512434293052345012398382631237312391259133489212?forum=powershellja

<改変>version2.0ではSet-Content -encoding byteが遅いので速度いまいち。ファイルに書き出します。画面ではみれません。
[byte[]]$crlf=@(13,10)
$WindowSize = 150
$File = [System.IO.File]::OpenRead("C:\Users\user\Desktop\FIXED150.txt")
$Stream = New-Object System.IO.BinaryReader $File
0 .. (($File.Length - 1) / $WindowSize) |% { $Stream.ReadBytes($WindowSize) + $CrLf} | SC C:\Users\user\Desktop\OUT_FIXED150.txt -encoding byte;$File.close()


<いっそ.NETプログラミング版>ファイルに書き出します。画面ではみれません。
$len = 150
[byte[]]$crlf=@(13,10)
[byte[]]$buf     #(=>これは不要 要素数指定いらない) =@(0..($len-1))
$oFin = New-Object System.IO.Filestream ("C:\Users\user\Desktop\FIXED150.txt",[System.IO.FileMode]::Open)
$oBRin = New-Object System.IO.BinaryReader $oFin

$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\outFIXED150.txt",([System.IO.FileMode]::Create),([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout
while ($True){
    $buf = $oBRin.ReadBytes($len)
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf)
    }
    $oBRout.Write($crlf)
}

$oFin.Close();$oFout.Close();


 

<改変2>
[byte[]]$crlf=@(13,10)
$WindowSize = 150
$File = [System.IO.File]::OpenRead("C:\Users\user\Desktop\FIXED150.txt")
$Stream = New-Object System.IO.BinaryReader $File
$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\outFIXED150_999.txt",([System.IO.FileMode]::Create),([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout
0 .. (($File.Length - 1) / $WindowSize) |% { $oBRout.Write($Stream.ReadBytes($WindowSize)); $oBRout.Write($CrLf)} ;$File.close();$oFout.close();

<4件で試した結果>

PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user>
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
150
PS C:\Users\user> $buf = $oBRin.ReadBytes($len)
PS C:\Users\user> $buf.length
0

<20181209追記END>

 

<20181211追記STA><分割試行>[int[]]$arr = @(2,15,10,8,115)で

measure-command{
$len = 150
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin = New-Object System.IO.Filestream ("C:\Users\user\Desktop\Fixed150.txt",[System.IO.FileMode]::Open)
$oBRin = New-Object System.IO.BinaryReader $oFin

$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\oout.txt",([System.IO.FileMode]::Create), ([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

while ($True){
    $buf = $oBRin.ReadBytes(2)
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf) ; $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
        $oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($crlf)
    }
}
$oFin.Close();$oFout.Close();
}

 

Days : 0
Hours : 0
Minutes : 0
Seconds : 28
Milliseconds : 179

 <20181211追記END>

 <20181211追記2STA>

measure-command{
$len = 1500
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin = New-Object System.IO.Filestream ("C:\Users\user\Desktop\Fixed150.txt",[System.IO.FileMode]::Open)
$oBRin = New-Object System.IO.BinaryReader $oFin

$oFout = New-Object System.IO.Filestream ("C:\Users\user\Desktop\ooooooout.txt",([System.IO.FileMode]::Create),([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

while ($True){
$buf = $oBRin.ReadBytes(2)
if($buf.length -eq 0){
break
}else{
$oBRout.Write($buf) ; $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($tab)

$oBRout.Write($oBRin.ReadBytes(002)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(015)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(010)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(008)); $oBRout.Write($tab)
$oBRout.Write($oBRin.ReadBytes(115)); $oBRout.Write($crlf)
}
}
$oFin.Close();$oFout.Close();
}

 <20181211追記2END>


 

<20181219追記STA>簡易版(少し遅くなります)
measure-command{
$len = 150
[int[]]$width = @(2,15,10,8,115)
$MAX  = ($width.length - 1)
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin   = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\Fixed150.txt",[System.IO.FileMode]::Open)
$oBRin  = New-Object System.IO.BinaryReader $oFin

 

$oFout  = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\oout2.txt",([System.IO.FileMode]::Create), ([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

 

while ($True){
    $buf = $oBRin.ReadBytes($width[0])
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf) ; $oBRout.Write($tab)

 

        for($i=1;$i -lt $MAX;$i++){
            $oBRout.Write($oBRin.ReadBytes($width[$i])); $oBRout.Write($tab)
        }
        $oBRout.Write($oBRin.ReadBytes($width[$MAX]))  ; $oBRout.Write($crlf)
    }
}
$oFin.Close();$oFout.Close();
}

 <20181219追記END>

 

<20181220追記STA>簡易版(少し遅くなります)CrLfありの固定長の分割
measure-command{
$len = 150 + 2   #"+ 2"はCrLf分
[int[]]$width = @(2,15,10,8,115)
$MAX  = ($width.length - 1)
[byte[]]$crlf=@(13,10);[byte[]]$tab=@(9);
[byte[]]$buf #(=>これは不要 要素数指定いらない)=@(0..($len-1))
$oFin   = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\crlfFixed150.txt",[System.IO.FileMode]::Open)
$oBRin  = New-Object System.IO.BinaryReader $oFin

$oFout  = New-Object System.IO.Filestream ("C:\Users\tkhs1732\Desktop\crlf_oout2.txt",([System.IO.FileMode]::Create), ([System.IO.FileAccess]::Write))
$oBRout = New-Object System.IO.BinaryWriter $oFout

while ($True){
    $buf = $oBRin.ReadBytes($width[0])
    if($buf.length -eq 0){
        break
    }else{
        $oBRout.Write($buf) ; $oBRout.Write($tab)

        for($i=1;$i -lt $MAX;$i++){
            $oBRout.Write($oBRin.ReadBytes($width[$i])); $oBRout.Write($tab)
        }
        $oBRout.Write($oBRin.ReadBytes($width[$MAX]))  ; $oBRout.Write($crlf)
        $DUMMY = $oBRin.ReadBytes(2) #末尾のCrLf分を読み取るだけの処理
    }
}
$oFin.Close();$oFout.Close();
}


<20181220追記END>

 



 


でっきるかな?PowerShell 004

2018年11月24日 01時27分29秒 | PowerShell

たっぷす庵 様 「PowerShellでifの戻り値パイプ出来ないの巻」
空のパイプ要素は許可されていません。
for文でパイプ
即時関数

「Cookie の使用に同意するものと見なされます。」がでます。

https://stuncloud.wordpress.com/2016/01/13/powershell_cant_pipe_if/

 

環境 Windows7 32bit PowerShell version 2.0 

PS C:\Users\user\desktop> for($i=0;$i -lt 16;$i++){"ITドカタの世界へようこそ $i"}|out-file -encoding default a777.txt
空のパイプ要素は許可されていません。
発生場所 行:1 文字:46
+ for($i=0;$i -lt 16;$i++){"ITドカタの世界へようこそ $i"}| <<<
    + CategoryInfo          : ParserError: (:) []、ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

PS C:\Users\user\desktop>


PS C:\Users\user\desktop> &{for($i=0;$i -lt 16;$i++){"ITドカタの世界へようこそ $i"}}|out-file -encoding default a777.txt

PS C:\Users\user\desktop>

 

<20181127追記STA>PowerShellでSJIS固定長CrLf(改行)なしにCrLf(改行コード)を付加してレコード単位にする


インフラSEの運用・構築メモ 様
[PowerShell] テキストファイルをバイト単位で切り出す
http://rtaki.sakura.ne.jp/infra/?p=2783

 

(1)$enc = [System.Text.Encoding]::Default
(2)$bytes = $enc.GetBytes(($f_text = gc FIXED150.txt))
 (2-1)$f_text.length でレングス確認(文字数になる)
もしくは
   $bytes = $enc.GetBytes((gc FIXED150.txt))
 (2-2)$bytes.length  で確認(バイト数になる)
(3)$len = 150  #<=忘れると無限ループにはまります。要注意。
(4)&{for($i=0;  $i -lt $bytes.length; $i = $i + $len){$enc.GetString($bytes,$i,$len)}} | out-file out_fixed150.txt -encoding default

 <20181127追記END>

 

<20181129追記STA>PowerShellでCrLf(改行コード)なしSJIS固定長を項目単位に分割する。タブ区切り化。
PS C:\Users\user> cd desktop
PS C:\Users\user\desktop> $enc = [System.Text.Encoding]::Default
PS C:\Users\user\desktop> $bytes = $enc.GetBytes((gc FIXED150.txt))
PS C:\Users\user\desktop> $bytes.length
150000
PS C:\Users\user\desktop> $len = 150 #<=忘れると無限ループにはまります。要注意。
PS C:\Users\user\desktop> [int[]]$arr = @(2,15,10,8,115)
PS C:\Users\user\desktop> [string[]]$item = @(0..4)
PS C:\Users\user\desktop>

 

&{for($i=0; $i -lt $bytes.length; $i= $i + $len){
    $pos = $i
    for($j=0; $j -lt $arr.length; $j++){
        $item[$j] = $enc.GetString($bytes, $pos, $arr[$j])
        $pos = $pos + $arr[$j]
    }
    $item -join "`t"
 }
} | out-file a888.txt -encoding default

 

<20181129追記END>

<固定長の種>

'<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 = "FIXED150.txt"

    Set oFS = CreateObject("Scripting.FileSystemObject")

    Set oFW = oFS.OpenTextFile(TargetFile, ForWriting, True)

    For i = 1 To 1000
        'KBN = Int(Rnd()*5)
        KBN = "02"
        'CrLf付き
        'oFw.Writeline out_Area(KBN)
        'Crlf無し
        oFw.Write Right("00" & i,2)  & MID(out_Area(KBN),3,147)
    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" & _
                  "FあFFいいFFFう#" & _
                  "GかGGきGG#" & _
                  "HHHさHH#" & _
                  String(114,"J") & "#"
                  '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

<20181130追記STA>

文字列を特定の長さで改行する方法

「Linuxのfoldコマンドと同等のことを、PowerShellで行いたい」

https://social.technet.microsoft.com/Forums/ja-JP/d962931a-55cc-4bc5-9661-d554c78137ae/25991233832101512434293052345012398382631237312391259133489212?forum=powershellja

関係ないけど、パック10進数含むようなバイナリデータを扱うなら、perlが一番入手しやすくて、短く書けて、速くていいとおもいます。

perl58.dll <=versionでかわります。
perl.exe

さえあれば動きます。

「くんすとの備忘録」様
http://kunst1080.hatenablog.com/entry/2014/04/10/233257

固定長を区切るのも、以下は無理やりironRubyで書きましたけど、perlでできるし。

パック10進数を文字形式に変換するのも、そんなにむずかしくはないはず。

<自己記事>IronRubyでSJIS改行(CrLf)なし固定長めった斬り

https://blog.goo.ne.jp/tkhs1732/e/9edb7bb22c879f2076abc4e24e5b47a2

あとは、VB.NET か C#.NET。 さくっとその目的専用のコードを書いて(汎用なfoldをつくるのではない。作っていただけるとありがたいです。)、コンパイルして、exe作るとか?

<20181130追記END>


 

<20181201追記STA>全角"<" ">" を 半角"<" ">"にしてください。

PowerShellではありません。Perlですが、項目数が少なく かつ データ件数も10000件くらいなら


jperl -bpe "s/(.{2})(.{15})(.{10})(.{8})(.{115})/$1\t$2\t$3\t$4\t$5\n/g;"  FIXED150.TXT smosmo.txt


perl -pe "s/(.{2})(.{15})(.{10})(.{8})(.{115})/$1\t$2\t$3\t$4\t$5\n/g;"  FIXED150.TXT smosmo2.txt

$1~$10まで


こんなのでもいけます。

鬼車SED

onigsed -R --ctype=ASCII "s/(.{2})(.{15})(.{10})(.{8})(.{115})/\1\t\2\t\3\t\4\t\5\n/g;" FIXED150.TXT hiyohiyo.txt

下記どちらも\1~\9まで  

-r, --regexp-extended 
    use extended regular expressions in the script.
-R, --regexp-perl
     use Perl 5's regular expressions syntax in the script.

<20181201追記END>

<20181207追記STA>PowerShell
環境 Windows10 64bit PowerShell version 5.0 にて動かす機会があった。

性能のいいマシンならそこそこ動くと思われる。

 

Windows7 32bit PowerShell version 2.0 の低スペックノートPCでは、

 

メモリを大量消費の上、速度もコード量の割に遅い

[foldで150バイト風]

[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_ + $crlf} | sc out_Fixed150.txt -encoding byte


[1レコード先頭35バイトを取り出す]
[byte[]]$crlf = @(13,10)
gc fixed150.txt -encoding byte -readcount 150  -totalcount (150*200) |%{$_[0..34] + $crlf} | sc out_Fixed150.txt -encoding byte

<20181207追記END>




でっきるかな?PowerShell 003

2018年11月04日 08時38分31秒 | PowerShell

郵便番号データcsvをソートしてみる

環境 Windows7 32bit PowerShell version 2.0

$header ="a01","a02","a03","a04","a05","a06","a07","a08","a09","a10","a11","a12","a13","a14","a15"

PS C:\Users\user\desktop> gc 43KUMAMO.CSV |convertfrom-csv -header $header |sort a03 |convertto-csv |out-file -encoding default hoge2.txt

out-file -encoding default をいれないと Unicode(UTF-16 LE)になる

Sor-Objectで複数キー Asc Desc 指定できるみたいだけど、versionの為かうまくいかない

うまくいったら追記する

キーワード PowerShell ソート 複数キーでいっぱいひっかかるので参考にされたし

PS C:\Users\user\desktop> get-help Sort-Object -detailed

にでてる。長いので割愛。