0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBScriputによるCSVファイルの加工

0
Posted at

■経緯

 ・CSVデータをある程度自由に加工するスクリプトが欲しかった

■環境:

 ・windows10 64bit

■やりたいこと:

・CSVデータ(テキスト)で、一番の左の列の文字を加工したい
 例)"id" → "id01"

■VBSの実装方針

 ・UTF-8に対応する事
 ・引数で入出力のファイル名を渡せる事
例) cscript.exe readwrite-csv modules.csv outmodules.csv
  ※実行はcscript.exeを用いて確認
 ・将来的に簡単な改造でファイルを分割したりする事が出来る事
 ・結果をプロンプト上で確認出来る事

■コード

readwrite-csv
Option Explicit
'-------------------------------------- 
'main
'-------------------------------------- 
    Dim inputFileName,outputFileName
    Dim inStream,outStream
    Dim records,intY,out,m,debugout
    '入出力ストリームの生成・設定(テキスト、UTF-8)
    inputFileName = WScript.Arguments.Item(0)
    outputFileName = WScript.Arguments.Item(1)
    Set inStream = CreateObject("ADODB.Stream")
    Set outStream =CreateObject("ADODB.Stream")
    inStream.type = 2 '1:バイナリデータ 2:テキストデータ
    inStream.charset = "UTF-8" '文字コード設定
    inStream.open
    inStream.LoadFromFile inputFileName
    outStream.type = 2
    outStream.charset = "UTF-8"
    outStream.open 

    Do While inStream.EOS = False
        '読み込んだレコードをカンマ区切りで配列に格納
        records = Split(inStream.ReadText(-2), ",") 'ReadTextの第一引数:-1:全部読み込む -2:一行読み込む
        intY = 1
        out = ""
        For Each m In records
            if intY = 1 then
                m = Deldbquot(m,"""")
                m = """" + m + "01" + """"
            End If
            out = out & " " & m
            intY = intY + 1
        Next
        debugout = debugout & out & VbCrLf 'debug用
        outStream.WriteText out, 1 'WriteTextの第二引数:0:文字列のみ書き込む 1:文字列+改行を書き込む
    Loop
    'debug用
    WScript.StdOut.WriteLine "--------------------------------------" 
    WScript.StdOut.WriteLine outputFileName
    WScript.StdOut.WriteLine "--------------------------------------"
    WScript.StdOut.WriteLine debugout
    outStream.SaveToFile outputFileName, 2 '1:ファイルがない場合はファイル作成 2:ファイルがある場合は上書き
    inStream.Close
    outStream.Close
'-------------------------------------- 
'関数、文字削除
'-------------------------------------- 
Function Deldbquot(strString,delString)
    Deldbquot = Replace(strString, delString, "")
End Function

■デバッグ

実行すると、左の列最後に”01”が追加された

C:\Bitnami\test>cscript.exe readwrite-csv.vbs modules.csv outmodules.csv
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.


outmodules.csv

"id01" "project_id" "name"
"101" "1" "issue_tracking"
"201" "1" "time_tracking"
"301" "1" "news"
"401" "1" "documents"
"501" "1" "files"
"601" "1" "wiki"
"701" "1" "repository"
"801" "1" "boards"
"901" "1" "calendar"
"1001" "1" "gantt"

C:\Bitnami\test>

■工数

 2h

以上です、お疲れ様です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?