コマンドラインからMDB/ACCDBファイルのテーブル一覧とカラム名一覧を表示する

前回「コマンドラインからMDB/ACCDBファイルのデータをSQLで抽出する」でデータの抽出ができるようになったけれど、テーブル名とかカラム名とかわからないとSQLが書けない。
そんなわけで、tablelist.vbsというスクリプトを書いてみた。
option explicit
dim arg : set arg = WScript.Arguments
if arg.count = 0 then
    wscript.echo "引数に mdbまたはaccdbファイル を指定してください"
    wscript.quit
end if

dim cn,cat,mdbfile
mdbfile = arg(0)
set cn  = createObject("ADODB.Connection")
with cn
    mdbfile =trim(mdbfile)
    if right(mdbfile,4) = ".mdb" then
        .Provider = "Microsoft.Jet.OLEDB.4.0"
    end if
    if right(mdbfile,6) = ".accdb" then
        .Provider = "Microsoft.ACE.OLEDB.12.0"
    end if
    .Properties("Data Source") = mdbfile
end with
cn.open

set cat = createObject("ADOX.Catalog")
cat.ActiveConnection = cn

dim table, tablename, column, cType, sTypename
select case (arg.count)
case 1
    for each table in cat.tables
        if table.type="TABLE" then
            wsh.echo table.name
        end if
    next
case 2
    set cType = createobject("Scripting.Dictionary")
    cType.add   3, "Integer_I8"
    cType.add   5, "Double_R8"
    cType.add   7, "DateTime"
    cType.add 202, "Text"
    cType.add 203, "Memo"

    tablename = arg(1)
    wsh.echo "Name" & vbTab & "Type"
    set table = cat.tables(tablename)
    for each column in table.columns
        sTypename = column.Type
        if cType.exists(column.type) then sTypename = cType(column.type)
        wsh.echo column.name & vbTab & sTypename
    next
end select

cn.close

テーブルの一覧を表示したいときは次のようにする。
cscript //nologo tablelist.vbs database.mdb

テーブル内のカラム一覧を表示したいときは次のようにする。
cscript //nologo tablelist.vbs database.mdb tablename

ただし、カラムの型名は全部入力するのが面倒なので、手元のMDBファイル内で使用されていた型以外は数字で表示される。

この記事へのコメント

この記事へのトラックバック