本文主要是介绍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 SS = NewLine & "Path:" & TabStop & File.PathS = S & NewLine & "Name:" & TabStop & File.NameS = S & NewLine & "Type:" & TabStop & File.TypeS = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File)S = S & NewLine & "Created:" & TabStop & File.DateCreatedS = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessedS = S & NewLine & "Modified:" & TabStop & File.DateLastModifiedS = S & NewLine & "Size" & TabStop & File.Size & NewLineGenerateFileInformation = SEnd FunctionFunction GetFolderLastModifyTime (FolderPath)If (FSO.FileExists("tags")) ThenSet File = FSO.GetFile("tags")lastModTime = File.DateLastModified'MsgBox lastModTimeElsemsg = filespec & " doesn't exist."'MsgBox msgEnd IfSet Folder = FSO.GetFolder(FolderPath)folderModTime = Folder.DateLastModifiedlTime = Folder.DateLastModifiedSet Files = Folder.FilesFor Each File In FilesIf lTime < File.DateLastModified ThenlTime = File.DateLastModifiedEnd IfNextGetFolderLastModifyTime = lTime
End FunctionFunction FlashTagFile (tagFileName, FolderPath)If (FSO.FileExists(tagFileName)) ThenSet File = FSO.GetFile(tagFileName)lastModTime = File.DateLastModifiedElsestrCmd = "ctags.exe --langdef=logic --langmap=logic:.logic --regex-logic=""/^\s*(Logic|Join)\s+(\w+)\s+\{/\2/L,Logic/"" -n -R " & FolderPathoExec = WshShell.Run(strCmd, SW_HIDE, True)FlashTagFile = TrueExit FunctionEnd IfIf lastModTime < GetFolderLastModifyTime(FolderPath) ThenstrCmd = "ctags.exe --langdef=logic --langmap=logic:.logic --regex-logic=""/^\s*(Logic|Join)\s+(\w+)\s+\{/\2/L,Logic/"" -n -R " & FolderPathoExec = WshShell.Run(strCmd, SW_HIDE, True)FlashTagFile = True ElseFlashTagFile = FalseEnd IfEnd FunctionFunction LocationLogic (logicName, logicFileName, lineNum)LocationLogic = "It's just a empty Function now!" & vbCrLf _& "It will find [" & logicName & "] in file [" & logicFileName & "]. line : " & lineNumEnd FunctionFunction GetLogicInfo (logicName, tagFileName)Dim oRe, oMatch, oMatchesSet tagFile = FSO.GetFile(tagFileName)Set TextStream = FSO.OpenTextFile(tagFileName, OpenFileForReading)allLogicInfo = TextStream.ReadAllTextStream.Close' the format as below'testLogic010 logic\Logic0.logic 24;" LSet oRe = New RegExpoRe.Pattern = logicName & "\s+(.*?)\s+(\d+);""\s+(\w)"' 得到 Matches 集合oRe.IgnoreCase = False ' 设置是否区分大小写。oRe.Global = False ' 设置全程匹配。Set oMatches = oRe.Execute(allLogicInfo)If oMatches.Count = 0 ThenMsgBox "Can not find this logic <" & logicName & ">"GetLogicInfo = ""Exit FunctionElse Set oMatch = oMatches(0)filePath = oMatch.SubMatches(0)lineNum = oMatch.SubMatches(1)End IfGetLogicInfo = LocationLogic(logicName, filePath, lineNum)Set oRe = NothingEnd FunctionFunction GetLogicNameFromGTR ()' It is empty nowGetLogicNameFromGTR = "testLogic99956"End FunctionFunction GetLogicFilePath ()' 这种方式能够读取的注册表有限制,功能更强的方式是采用winmgmts对象读取,但是没有这个简单GetLogicFilePath = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Evernote\Installer")GetLogicFilePath = "logic"End FunctionFunction InsertLogicInfoToScript (logicInfo)' It is empty nowInsertLogicInfoToScript = "It is empty now"End FunctionFunction OpenLogicFileInGTR ()' 查找逻辑文件及逻辑所在行数的方法与函数GetLogicInfo一样InsertLogicInfoToScript = "It is empty now"End FunctionFunction Main ()Set objArgs = WScript.ArgumentsIf objArgs.Count > 0 ThenshowMethod = objArgs(I)End IflogicName = 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 logicInfoCase OPEN_LOGIC_FILE OpenLogicFileInGTRCase Else WshShell.popup logicInfo, 10, "逻辑提示,10秒后消失", 64End Select
End FunctionStartTime = Timer
Main
EndTime = Timer
TimeIt = EndTime - StartTime
MsgBox "cast time " & TimeIt
这篇关于VBS脚本,中间用到了遍历文件夹,正则匹配。使用了 WScript.Shell 对象的几个方法。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!