VBS脚本,中间用到了遍历文件夹,正则匹配。使用了 WScript.Shell 对象的几个方法。

这个脚本只满足了我要完成的工作的一部分内容,其余内容必须在公司才能完成及调试了。


脚本运行需要有如下条件:

与脚本在同一级目录下的 ctags.exe ,需要5.8以后版本的。主要是要支持正则,支持 - R选项后面带目录路径的。

同一级目录下要有logic文件夹,文件夹内要有符合语法的logic文件。


脚本如下:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 初始化一些全局变量
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim FSO
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 展示逻辑的方式
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const SW_LOGIC_IN_DLG = 0
Const INSERT_LOGIC_TO_SCRIPT = 1
Const OPEN_LOGIC_FILE = 2

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Window Style
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const SW_HIDE = 0
Const SW_SHOW = 1
Const SW_MIN = 2
Const SW_MAX = 3

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 由 Drive.DriveType 返回的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const DriveTypeRemovable = 1
Const DriveTypeFixed = 2
Const DriveTypeNetwork = 3
Const DriveTypeCDROM = 4
Const DriveTypeRAMDisk = 5

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 由 File.Attributes 返回的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FileAttrNormal  = 0
Const FileAttrReadOnly = 1
Const FileAttrHidden = 2
Const FileAttrSystem = 4
Const FileAttrVolume = 8
Const FileAttrDirectory = 16
Const FileAttrArchive = 32 
Const FileAttrAlias = 64
Const FileAttrCompressed = 128

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 用来打开文件的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const OpenFileForReading = 1 
Const OpenFileForWriting = 2 
Const OpenFileForAppending = 8 

Function GenerateFileInformation(File)

   Dim S

   S = NewLine & "Path:" & TabStop & File.Path
   S = S & NewLine & "Name:" & TabStop & File.Name
   S = S & NewLine & "Type:" & TabStop & File.Type
   S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File)
   S = S & NewLine & "Created:" & TabStop & File.DateCreated
   S = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessed
   S = S & NewLine & "Modified:" & TabStop & File.DateLastModified
   S = S & NewLine & "Size" & TabStop & File.Size & NewLine

   GenerateFileInformation = S

End Function

Function GetFolderLastModifyTime (FolderPath)

   If (FSO.FileExists("tags")) Then
      Set File = FSO.GetFile("tags")
      lastModTime = File.DateLastModified
      'MsgBox lastModTime
   Else
      msg = filespec & " doesn't exist."
      'MsgBox msg
   End If
    
    Set Folder = FSO.GetFolder(FolderPath)
    folderModTime = Folder.DateLastModified
        
    lTime = Folder.DateLastModified
    Set Files = Folder.Files
    For Each File In Files
        If lTime < File.DateLastModified Then
            lTime = File.DateLastModified
        End If
    Next
    
    GetFolderLastModifyTime = lTime
End Function

Function FlashTagFile (tagFileName, FolderPath)

    If (FSO.FileExists(tagFileName)) Then
        Set File = FSO.GetFile(tagFileName)
        lastModTime = File.DateLastModified
    Else
        strCmd = "ctags.exe --langdef=logic --langmap=logic:.logic --regex-logic=""/^\s*(Logic|Join)\s+(\w+)\s+\{/\2/L,Logic/"" -n -R " & FolderPath
        oExec = WshShell.Run(strCmd, SW_HIDE, True)
        FlashTagFile = True
        Exit Function
    End If
    
    If lastModTime < GetFolderLastModifyTime(FolderPath) Then
        strCmd = "ctags.exe --langdef=logic --langmap=logic:.logic --regex-logic=""/^\s*(Logic|Join)\s+(\w+)\s+\{/\2/L,Logic/"" -n -R " & FolderPath
        oExec = WshShell.Run(strCmd, SW_HIDE, True)
        FlashTagFile = True        
    Else
        FlashTagFile = False
    End If
    
End Function

Function LocationLogic (logicName, logicFileName, lineNum)

    LocationLogic = "It's just a empty Function now!" & vbCrLf _
                    & "It will find [" & logicName & "] in file [" & logicFileName & "]. line : " & lineNum
    
End Function

Function GetLogicInfo (logicName, tagFileName)

    Dim oRe, oMatch, oMatches
    
    
    Set tagFile = FSO.GetFile(tagFileName)
    Set TextStream = FSO.OpenTextFile(tagFileName, OpenFileForReading)
    allLogicInfo = TextStream.ReadAll
    TextStream.Close

    ' the format as below
    'testLogic010	logic\Logic0.logic	24;"	L
    Set oRe = New RegExp
    oRe.Pattern = logicName & "\s+(.*?)\s+(\d+);""\s+(\w)"
    ' 得到 Matches 集合
    oRe.IgnoreCase = False   ' 设置是否区分大小写。
    oRe.Global = False   ' 设置全程匹配。
    Set oMatches = oRe.Execute(allLogicInfo)
    If oMatches.Count = 0 Then
        MsgBox "Can not find this logic <" & logicName & ">"
        GetLogicInfo = ""
        Exit Function
    Else        
        Set oMatch = oMatches(0)
        filePath = oMatch.SubMatches(0)
        lineNum = oMatch.SubMatches(1)
    End If
    
    GetLogicInfo = LocationLogic(logicName, filePath, lineNum)
    
    Set oRe = Nothing
    
End Function

Function GetLogicNameFromGTR ()

    ' It is empty now
    GetLogicNameFromGTR = "testLogic99956"
    
End Function

Function GetLogicFilePath ()
    
    ' 这种方式能够读取的注册表有限制,功能更强的方式是采用winmgmts对象读取,但是没有这个简单
    GetLogicFilePath = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Evernote\Installer")
    GetLogicFilePath = "logic"
       
End Function

Function InsertLogicInfoToScript (logicInfo)
    
    ' It is empty now
    InsertLogicInfoToScript = "It is empty now"
       
End Function

Function OpenLogicFileInGTR ()
    
    ' 查找逻辑文件及逻辑所在行数的方法与函数GetLogicInfo一样
    InsertLogicInfoToScript = "It is empty now"
       
End Function

Function Main ()

    Set   objArgs   =   WScript.Arguments
    If objArgs.Count > 0 Then
        showMethod = objArgs(I)
    End If
    
    logicName = GetLogicNameFromGTR()
    
    FlashTagFile "tags", GetLogicFilePath()
        
    logicInfo = GetLogicInfo(logicName, "tags")
    
    Select Case showMethod
                                 ' 64 means show "Information Mark" icon.
        'Case SW_LOGIC_IN_DLG     WshShell.popup(logicInfo, 10, "逻辑提示,10秒后消失", 64)
        Case INSERT_LOGIC_TO_SCRIPT   InsertLogicInfoToScript logicInfo
        Case OPEN_LOGIC_FILE    OpenLogicFileInGTR
        Case Else  WshShell.popup logicInfo, 10, "逻辑提示,10秒后消失", 64
    End Select
End Function

StartTime = Timer
Main
EndTime = Timer
TimeIt = EndTime - StartTime
MsgBox "cast time " & TimeIt


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值