DIAdemでデータを保存するときに"\"を"/"に置き換える方法はありませんか?

更新しました Jun 4, 2021

使用製品

ソフトウェア

  • DIAdem

問題

DIAdemでデータを保存するときに"\"を"/"に置き換える方法はありませんか?チャンネル名に"\"が含まれているデータを*.csvファイルに出力すると、"\"のまま出力されています。
 
ExportCSV.png

サンプルのデータ名を上記のように編集し、下記のVBScriptを使用しています。
Option Explicit
dim targetPath : targetPath = CurrentScriptPath &"Example.csv"
dim channelsToSave : Set channelsToSave = Data.GetChannels("[1]/*")
dim Delimiter : Delimiter = ","
Call DataFileSaveSel("<filename>"&targetPath&"</filename>" & "<delimiter>"&Delimiter&"</delimiter>", "CSV", channelsToSave)

解決策

下記のVBScriptを使用し、ヘッダ(チャンネル名、単位)とボディ(データ)を別々に出力し、結合する必要があります。
Option Explicit
dim targetPath : targetPath = CurrentScriptPath &"Example.csv"
dim channelsToSave : Set channelsToSave = Data.GetChannels("[1]/*")
dim Delimiter : Delimiter = ","
call DataFileSaveSel("<filename>"&targetPath&"</filename>" & "<delimiter>"&Delimiter&"</delimiter>", "CSV", channelsToSave)

call SaveToCsvSpecial(channelsToSave, targetPath)

sub SaveToCsvSpecial(channelsToSave, targetPath)

  dim delimiter : delimiter = ","
  ' lines to be added in front
  dim nameLine : nameLine = ""
  dim unitLine : unitLine = ""
  
  ' loop over channels and create lead in
  dim firstTime : firstTime = true
  dim channelToSave : for each channelToSave in channelsToSave
    if firstTime then
      firstTime = false
    else
      nameLine = nameLine & delimiter
      unitLine = unitLine & delimiter
    end if
    
 nameLine = nameLine & Replace(channelToSave.name, "\", "/")
  unitLine = unitLine & channelToSave.UnitSymbol

  Next
 
  ' join them and add CRLF
  dim myHeader : myHeader = nameLine & VBCRLF & unitLine & VBCRLF
  ' lets write them to two files and join them
  call SaveToCsvWithOwnHeader(myHeader, channelsToSave, delimiter, targetPath)
end sub


sub SaveToCsvWithOwnHeader(byVal ownHeader, byVal channelsToSaveList, byVal delimiter, byVal targetPath)
  dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  
  dim targetPathBody : targetPathBody = targetPath & ".body"
  
  dim tfHandle : set tfHandle = fso.OpenTextFile(targetPath, 2, 0)
  tfHandle.Write(ownHeader)
  tfHandle.Close()
  
  dim param : param = "<filename>" & replace(targetPathBody, "&", "&amp;") & "</filename>" &_
    "<delimiter>" & delimiter & "</delimiter><channelnames>no</channelnames>"
    
  Call DataFileSaveSel(param, "CSV_EXPORT", channelsToSave)
  
  ' the copy command can join files binary
  dim cmd : cmd = "cmd /C copy /b """ & targetPath & """ + """ & targetPathBody & """ """ & targetPath & """"
  dim rv : rv = CreateObject("wscript.shell").Run(cmd, 7, True)
  if 0 <> rv then
    err.Raise 100, "", "copy /b failed with state " & rv
  end if
  ' lets remove the body file
  fso.DeleteFile targetPathBody
end sub
 
Exportbackslash.png

DIAdemの命名規則では一部の記号をデータセット、チャンネルグループ、チャンネル、カスタムプロパティの名前に使用した場合、自動的に置き換えが発生する仕様になっています。その為、例えばチャンネル名が"Speed (m/s)"のデータをDIAdem Data Portalに読み込むと、自動的に"Speed (m\s)"と変換されます。また、このデータを*.csvファイルなどに出力すると、"Speed (m\s)"のまま出力されます。これらの自動置き換えされた部分を元の状態に戻してファイル出力するには上記の様なコードが必要となります。