天天看點

PowerDesigner_使用vbs腳本将txt檔案導入pdm

Dim system, file

Set system = CreateObject("Scripting.FileSystemObject") '建立檔案對象

Dim ForReading, ForWriting, ForAppending

dim str '表字段

dim tittle '表的名字

dim first

ForReading   = 1 ' 設定檔案隻讀 

ForWriting   = 2 ' 設定檔案寫入

ForAppending = 8 ' 設定檔案追加

'-----------------------------------------------------------------------------

' 主要程式

'-----------------------------------------------------------------------------

Set file = system.OpenTextFile("C:\Users\Administrator\Desktop\pdm.txt", ForReading)'打開文本文檔

Dim noLine

Dim Tab  '定義一個表,vbscript中變量沒有那麼嚴格的類型,但此變量将來将用來表示table

ValidationMode = True

Dim mdl ' 定義目前激活的模型,也就是mdl

Dim Col '定義列

dim dm, dmstr '定義

Dim SSS '定義截取字元串的标準

Dim isNewTable '定義是否是新表

Dim i '記錄下第幾行,0代表第一行

Set mdl = ActiveModel '擷取目前激活模型

'-----------------------------------------------------------------------------

' 建表操作

'-----------------------------------------------------------------------------

set Tab = mdl.Tables.CreateNew

isNewTable = True

first=file.readline '讀文檔按行讀

tittle=split(first) '以空格分隔劃分入數組,擷取的是表的屬性,可以擷取更多屬性,根據實際情況而定

Tab.name=tittle(0)&tittle(1) 'name

Tab.code=tittle(1) 'code

Tab.comment=tittle(1) 'comment

'-----------------------------------------------------------------------------

' 向表裡的字段指派操作

'-----------------------------------------------------------------------------

Do While file.AtEndOfStream <> True '循環讀取文檔的每一行

 '獲得每個字段的資料

   SSS = file.ReadLine

   str=split(SSS)

  '判斷是不是新表

   if isNewTable = true then

     if SSS <> "" then '如果擷取的是字元串,則不見新表

       isNewTable = False

     end if

   elseif SSS = "" then '如果擷取的是空格,

     set Tab = mdl.Tables.CreateNew  '建立新表,這是讀到空行時的表現,自己用來警示,是以txt文檔中的表與字段之間不要有空行

     SSS = file.ReadLine

     tittle= split(SSS)

     if ubound(tittle)=-1 then 

     SSS = file.ReadLine

     MsgBox SSS

     d

     end if

      Tab.name=tittle(0)&tittle(1) 'name

      Tab.code=tittle(1) 'code

      Tab.comment=tittle(1) 'comment

      'SSS = file.ReadLine

      i=0

      isNewTable=true

   else  

     set Col=tab.Columns.CreateNew '建立一行字段 (若不成功,預設建立7行字段)

    '依次設定屬性,同表的屬性,字段熟悉也可以設定更多,根據實際情況

     Col.name = str(1) '字段名稱

     Col.Code= str(2) '字段說明

     if str(3)="VARCHAR" Or str(3)="NVARCHAR" then 

     Col.datatype=str(3)&"("&str(4)&")" '字段類型

     else

     Col.datatype=str(3)

     end if

'-----------------------------------------------------------------------------

' 當是第一行的時候設為主鍵,自己寫的,i到後面可以添加外鍵和精度使用

'-----------------------------------------------------------------------------

     'MsgBox str(3)

     'Col.precision=cint(str(4)) '字段精度

     'if i=0 then 

     'Col.primary=true'設定主鍵

     'end if

     if str(1)="檔案号" Or str(1)="檔案序号" then

     Col.primary=true'設定主鍵

     end if

     i=i+1

   end if 

Loop 

file.Close
           

txt檔案模闆:

表名1 TB1
序号 字段名稱 字段代碼 字段類型 字段長度 值域 限制條件 備注
1 NAME NAME VARCHAR 50 非空 M
           

引用連接配接