LoginSignup
1
4

More than 5 years have passed since last update.

VBSで特定のフォルダ内にある連番ファイルの中身を結合し、txtにExport

Last updated at Posted at 2017-07-11

VBSで特定のフォルダ内にある連番ファイルの中身を結合し、テキストに吐き出すスクリプトを書いた。
久しぶりに触ったけど、qiitaに色々リファレンスがあったので、あまり苦労せず作成できた。

書き方で甘い部分があると思うので、ご指摘等頂ければ、たいへん嬉しいです!

' ファイル操作のためのおまじない
dim fso
set fso = WScript.CreateObject("Scripting.FileSystemObject")

' カレントパスを取得
dim current_path
current_path = fso.getParentFolderName(WScript.ScriptFullName)

' 今いるディレクトリ内にあるファイル名一覧を取得
dim folder
set folder = fso.getFolder(current_path)

'結果保存用のtxtデータが存在した場合、削除
if fso.FileExists("output_text.txt") Then
    fso.DeleteFile "output_text.txt", True
end if

'エラー結果保存用のtxtデータが存在した場合、削除
if fso.FileExists("output_error_text.txt") Then
    fso.DeleteFile "output_error_text.txt", True
end if

' 結果保存用のtxtデータを追加上書きモードでOPEN
dim output_file
set output_file = fso.OpenTextFile("output_text.txt", 8, True)

' エラー結果保存用のtxtデータを追加上書きモードでOPEN
dim output_error_file
set output_error_file = fso.OpenTextFile("output_error_text.txt", 8, True)

' ファイル一覧にあるファイルをリストにしてループ処理
dim file
for each file in folder.files

    'ファイル名がTANADATAから開始しており、かつナンバリングされているものを対象にする。下記のfunction file_name_checkを参照。
    if file_name_check(file.name) = True Then 

        dim input_file
        set input_file = fso.OpenTextFile(file.name, 1, False, 0)

    '各ファイルに含まれるデータを1行ずつ読込み
        do until input_file.AtEndOfStream

            dim line_str
            line_str = input_file.ReadLine

         ' 読込んだデータを精査し、数字で始まるデータのみ抽出(中に余計なテキストが含まれている事があるため)。下記のfunction line_checkを参照。
            if line_check(line_str) = True then

           ' 読込んだデータを精査。フォーマットが999999,999999,9999999か否かで、エラーデータがそうでないかを判別し、output_error_textかoutput_textに書込み。下記のfunction data_format_checkを参照。
                if data_format_check(line_str) = True then 'Exclude lines not include, digit information
                    output_file.WriteLine line_str
                else
                    output_error_file.WriteLine line_str
                end if
            end if
        loop
    end if
next

' 各ファイルをクローズ
input_file.Close
output_file.Close
output_error_file.Close

function file_name_check(name)
    dim re
    set re = new regexp  'Create the RegExp object

    re.Pattern = "^TANADATA(\s)*(\d)*$" ' Check file name, TANADATA 999...
    file_name_check = re.Test(name) 
end function

function line_check(line)
    dim re
    set re = new regexp  'Create the RegExp object

    re.Pattern = "^\d.+$" ' Get only digit data
    line_check = re.Test(line) 
end function

function data_format_check(line)
    dim re
    set re = new regexp  'Create the RegExp object

    re.Pattern = "^(\d){6},(\d){6},(\d){7}$" ' Check data format = 999999,999999,9999999
    data_format_check = re.Test(line) 
end function

参考

WSHでスクリプトファイル自身が存在するフォルダパスを取得する
http://qiita.com/asterisk9101/items/54cdcedb9ef60ea0bb21

VBS: ファイル一覧とフォルダ一覧の取得
http://qiita.com/asterisk9101/items/a7310c0e4ec33352835f

ファイルが存在するかを調べる
http://vbscript.infopos.net/p/000019.html

[ファイルを削除する]
http://www.whitire.com/vbs/tips0088.html

Clinick's Clinic on Scripting: 正規表現による Visual Basic Scripting Edition (VBScript) の機能強化
https://msdn.microsoft.com/ja-jp/library/ms974570.aspx

1
4
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
1
4