PowerDesigner技巧集5 ---- 用vbscript让txt文本中的内容导入PDM模型

如下的脚本实现用vbscript让txt文本中的内容导入PDM模型

Option Explicit

Dim system, file
Set system = CreateObject("Scripting.FileSystemObject") 

Dim ForReading, ForWriting, ForAppending
dim str
dim title
dim first
ForReading   = 1 ' 设置文件只读 
ForWriting   = 2 ' 设置文件写入
ForAppending = 8 ' 设置文件追加

Set file = system.OpenTextFile("E:\Users\hxw\Desktop\VBScript\tab.txt", ForReading)'打开文本文档
Dim noLine
Dim Tab  '定义一个表,vbscript中变量没有那么严格的类型,但此变量将来将用来表示table
ValidationMode = True
Dim mdl ' 定义当前激活的模型,也就是mdl
Dim Col
dim dm, dmstr
Dim SSS
Dim isNewTable

Set mdl = ActiveModel '获取当前激活模型

	set Tab = mdl.Tables.CreateNew
	isNewTable = True

	first=file.readline '读文档按行读
	title=split(first) '以空格分隔划分入数组,获取的是表的属性,可以获取更多属性,根据实际情况而定
	tab.name=title(0) 'name
	tab.code=title(1) 'code
	tab.comment=title(2) 'comment

Do While file.AtEndOfStream <> True '循环读取文档的每一行
   SSS = file.ReadLine
   str=split(SSS)

   If SSS <> "" Then
	 	isNewTable = False
   Else
    isNewTable = True
   End If
   
	If isNewTable = True Then
		first=file.readline '读文档按行读
	  title=split(first)
		set Tab = mdl.Tables.CreateNew  '创建新表,这是读到空行时的表现,自己用来警示
		tab.name=title(0) 'name
		tab.code=title(1) 'code
		tab.comment=title(2) 'comment
	Else
		set Col = tab.Columns.CreateNew '创建一行字段
		Col.name = str(0) '依次设置属性,同表的属性,字段熟悉也可以设置更多,根据实际情况
		Col.Code = str(0)
		col.datatype = str(2)
		col.comment = str(3)
   End If
Loop
file.Close


tab.txt的内容如下:

表名1 表code1 表comments1
字段名1 mycode1 VARCHAR2(32) 说明1
字段名2 mycode2 VARCHAR2(256) 说明2

表名2 表code2 表comments2
字段名1 mycode1 VARCHAR2(32) 说明1
字段名2 mycode2 VARCHAR2(256) 说明2

 

 

 

你可能感兴趣的:(File,脚本,System,文档,VBScript,Comments)