需求:对Systemwalker Operation Manager软件导出的文本文件进行处理,文本格式如下(敏感信息使用XX代替)
NET
jobnetname XX
jobnetcomment XX
noexecution XX
holidayshift XX
execattr XX
job 130
;
JOB
jobname sh XX.sh
jobnumber 1
jobname_jes XX
jobcomment Command
directory XX
req_host XX
queuename XX
execuser XX
iconposition 1,1
;
JOB
jobname sh XX.sh
jobnumber 2
jobname_jes XX
jobcomment Command
directory XX
req_host XX
queuename XX
execuser XX
iconposition 1,2
;
JOB
jobname sh XX.sh
jobnumber 3
jobparam XX XX
jobname_jes XX
jobcomment Command
directory XX
req_host XX
queuename XX
pre_job 1
execuser XX
jobattr XX
iconposition 2,1
;
.....
130
;
JOB
jobname sh XX.sh
jobnumber 1
jobname_jes XX
jobcomment Command
directory XX
req_host XX
queuename XX
execuser XX
iconposition 1,1
;
JOB
jobname sh XX.sh
jobnumber 2
jobname_jes XX
jobcomment Command
directory XX
req_host XX
queuename XX
execuser XX
iconposition 1,2
;
JOB
jobname sh XX.sh
jobnumber 3
jobparam XX XX
jobname_jes XX
jobcomment Command
directory XX
req_host XX
queuename XX
pre_job 1
execuser XX
jobattr XX
iconposition 2,1
;
.....
要求输入xx.sh可以得到她后面的所有xx.sh,安装顺序排列,并且将这些xx.sh替换成Excel文档里面定义的序号;
成果物是EXCEL文件,所以本次想通过VBA语言来进行编写处理;
VBA基础:
1、变量的定义:
参考: https://blog.youkuaiyun.com/bangemantou/article/details/70183048
2、函数的定义与调用:
代开VBA编辑器快捷键:ALT+CTRL
代码运行快捷键:F5
sub称为过程, function称为函数,他们之间的唯一区别是function有返回值。
3、循环与判断:
4、EXCEL对象:
5、文件的相关操作:
代码如下:
' 需求: 对Systemwalker Operation Manager软件导出的文件进行处理,要求输入一个shell名称可以知道其后面相关的shell名称
' 变量的定义
' VBA里面不支持结构体里面定义数组
' jobnet定义(自定义类型)
Type job
jobnetname As String ' jobnetname MAB1D001
messagename As String ' messagename MSGMAB1D0011,MSGMAB1D0012
jobnetcount As String ' job 36 jobnet的总数量
' jobname jobschmsgclear -net GYOMU01/MAB1D001 1
' jobname jobschmsgevent MSGMAB1D0011:GYOMU01 2
' jobname sh mab1d001001.sh 3
' jobname FLOW0091 4
' jobname echo " " 以及其他 name = echo 5
' jobname sh CrtDir.sh
jobname As String
jobcd As String
jobnumber As String ' jobnumber 3 job的顺序
jobpre As String ' pre_job 2,3 有可能有 有可能没有
End Type
Dim jobArray() As job '定义数组
Const DIR_PATH As String = "E:\jobnet\" '定义常量
' 函数定义
' 打开一个文本文件,将里面的值存入到jobnet中
' 正则表达式函数
' jobCd 获得值的类型
' str 输入字符串
Function getString(ByVal jobCd As String, ByVal str As String) As String
Dim returnStr As String
Dim reg As Object ' 正则表达式
Dim intTemp As Integer
Dim strtemp
Set reg = CreateObject("vbscript.regexp")
reg.Global = True
' jobnetname
If jobCd = "jobnetname" Then
reg.Pattern = "^jobnetname (.*)" ' 设定匹配模式
Set matchs = reg.Execute(str)
If matchs.Count <> 0 Then
returnSt