请求清理和错误的一点帮助

时间:2014-04-17 17:23:57

标签: vbscript code-formatting syslog

我被赋予了一个创建脚本的任务,该脚本接受日志文件(日期在文件名中),拉取数据并在事件管理器中发布。我有一个脚本,因为我应该知道脚本是丑陋的所以请温柔。我正在寻找两件事。

  1. 有些日子什么都没发生,并且没有创建当天的日志。当发生这种情况时,我的脚本会导致PC中的各种缓慢。如果没有新的文件添加到logs文件夹,我需要一些方法让脚本不执行任务。
  2. 我想帮助清理脚本。
  3. 就像我说我对此非常陌生,我使用网络上的脚本并让他们做我需要他们做的事情。 任何帮助都会受到很大的关注。

    Option Explicit
    
    Const ForReading = 1
    
    Dim strfolder
    Dim FSO
    Dim FLD
    Dim fil
    Dim strOldName
    Dim strNewName
    Dim strFileParts
    Dim objShell
    Dim objFSO
    Dim objFolder
    Dim strFileName
    Dim objFile
    Dim objTextFile
    Dim strNextLine
    Dim arrServiceList
    Dim i
    
    
    strFolder = "C:\Logs\"
    
    
    Set objShell = CreateObject ("Wscript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(strFolder)
    Set objShell = CreateObject ("Wscript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile ("C:\Logs\logCatchAll.log", ForReading)
    
    For Each strFileName in objFolder.Items
        If len(objFSO.GetExtensionName(strFileName)) > 0 Then
            Set objFile = objFSO.GetFile(strFolder & strFileName.Name)
            If DateDiff("H",objFile.DateLastModified,Now()) > 24 Then
                objFSO.DeleteFile(strFolder & strFileName.Name),True
            End If
        End If
     next
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FLD = FSO.GetFolder(strfolder)
    For Each fil in FLD.Files
        strOldName = fil.Path
        If InStr(strOldName, "-") > 0 Then
            strFileParts = Split(strOldName, "-")
            strNewName = strFileParts(0) & ".log"
            FSO.MoveFile strOldName, strNewName
        End If
     Next
     Set FLD = Nothing
     Set FSO = Nothing   
    
    
     Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    For i = 3 to Ubound(arrServiceList)
    objshell.LogEvent 4, arrServiceList(i)
     Loop 
    

1 个答案:

答案 0 :(得分:1)

  1. 您可以屏蔽Dim'd变量
  2. 您正多次重新激活objShell
  3. 您的代码底部有一个for循环,没有Next语句。
  4. 您不需要遍历日志文件,直到它到达AtEndOfStream,只需先将其存储在变量中。
  5. 如果您没有重置对象,则可以多次使用相同的objFSO。
  6. 您需要包含错误处理,以便了解代码中断的位置。

  7. 修改后的代码。

    Option Explicit
    
    'Handle errors manually.
    On Error Resume Next
    
    'Set Constants
    Const ForReading = 1
    
    'Set Strings
    Dim strFolder, strOldName, strNewName, strFileName, strFileParts, strNextLine, TFStrings
    strFolder = "C:\Logs\"
    
    'Set Objects
    Dim objShell, objFSO, objFolder, objFile, objTextFile
    Set objShell = CreateObject ("Wscript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objShell.Namespace(strFolder)
    TFStrings = split(objFSO.OpenTextFile("C:\Logs\logCatchAll.log", ForReading).ReadAll, vbcrlf)
    
    'Set Other Variables
    Dim FLD, fil, arrServiceList, i, executed
    executed = false
    
    
    'Delete file procedure...
    For Each strFileName in objFolder.Items
        If len(objFSO.GetExtensionName(strFileName)) > 0 Then
            Set objFile = objFSO.GetFile(strFolder & strFileName.Name)
            If DateDiff("H",objFile.DateLastModified,Now()) > 24 Then
                objFSO.DeleteFile(strFolder & strFileName.Name),True
            executed = true
            End If
        End If
    Next
    
    If executed then
        If err.number <> 0 then
            'File was found, but delete was unsuccessful, log failure of delete. 
            executed = false
            err.clear
        Else
            'Delete file procedure executed successfully. Lets move on. 
            executed = false
        End If
    Else
        'No file was found within the conditions. log failure of search. 
    End if
    
    'Move file and rename procedure...
    Set FLD = objFSO.GetFolder(strfolder)
    For Each fil in FLD.Files
       strOldName = fil.Path
       If InStr(strOldName, "-") > 0 Then
           strFileParts = Split(strOldName, "-")
           strNewName = strFileParts(0) & ".log"
           objFSO.MoveFile strOldName, strNewName
           executed = true
       End If
    Next
    Set FLD = Nothing
    Set FSO = Nothing   
    
    If executed then
        If err.number <> 0 then
            'File was found, but move was unsuccessful, log failure of move. 
            executed = false
            err.clear
        Else
            'Move file procedure executed successfully. Lets move on. 
            executed = false
        End If
    Else
        'No file was found within the conditions. log failure of search.
    End if
    
    For Each line in TFStrings
        strNextLine = line
        arrServiceList = Split(strNextLine , ",")
        For i = 3 to Ubound(arrServiceList)
            objshell.LogEvent 4, arrServiceList(i)
        Next
    Next