VBScript会丢弃某些文件扩展名

时间:2015-09-04 11:18:23

标签: vbscript

我的VBScript检查某个文件的时间戳是否比参数中传递的时间长。

如果它较旧,则参数允许,则此脚本创建的日志文件的状态将设置为STATUS =" ERROR"。当STATUS =" ERROR"然后将错误日志的尾部(10行代码)写入日志文件。到目前为止,这是有效的。

现在的问题是,在运行脚本的文件夹中,还有更多的.log和.txt文件。也可以遇到.zip或.rar文件。

如果其中一个引发错误,则脚本在运行tail时会显示以下内容

œšF§p#ýÃZ§‘KnÄÈÙCÓÈ7Ò-Ã"œs#GNM£S¸‘þa­ÓÈ%7bäì¡iä‚é–a‘F¯Îüm‹™Êh f"Ò>¨­Û%þ#N™«Q,ø Ð}e
    ·v–­‰³‘$j9Õ‡ó–i;!žBÉFëîÑ>
     p“Ò(ä3óÍ.x;…&µb6òhj˜æ '½3Izô
      ëùÿzjsÁ Æ÷vÌ‚F®Qe{cÍË<‹ù‰É1²F†y¿Ð"ÂÄ8jãVÒ«

这当然不是我想看到的。

问题是:

  • 有没有办法让脚本忽略除.log和.txt文件之外的其他文件扩展名,然后只是当文件扩展名是其他东西时才插入字符串消息?

  • 有没有办法让脚本打开.zip和.rar文件获取最新文件并在此文件上运行tail.exe文件?

if(status = "ERROR") then
    'Runs the tail.exe file to get the last 10 lines of text in the [sNewestFile] and insert them to the log file.
    'This will only be done IF there is a status = "ERROR"
    errorStr = WScript.CreateObject("WScript.Shell").Exec( _ 
           "tail -n 10 """ & sNewestFile & """" _ 
       ).StdOut.ReadAll
    objLogFile.writeline "" & vbCrLf
    objLogFile.writeline "Error Message Start" & vbCrLf
    objLogFile.writeline "" & errorStr & vbCrLf
    objLogFile.writeline "Error Message End"
End if

注意:

  • 我在这里获得了解决方案的帮助:https://stackoverflow.com/a/32352356/3430698
  • 这两个变量是sNewestFilesOldestFile,其中包含最新和最旧的文件。它们包含扩展名为sNewestFile = oFile.Path
  • 的文件的完整路径
  • 我有一个filespec变量,作为文件扩展名的参数传入。所以我试图围绕上面的代码运行if句子,检查状态是否为&#34; ERROR&#34;,if句子是否要检查

    if (filespec <> ".txt" or file <> ".log") then 
        writeline "something" 
    else 
        'run the tail on the file 
    

2 个答案:

答案 0 :(得分:4)

  

有没有办法让脚本忽略除.log和.txt文件之外的其他文件扩展名

是。有FileSystemObject的GetExtensionName function

ext = LCase(FSO.GetExtensionName(file))

Select Case ext
    Case "log", "txt"
        ' we have a text file
    Case "zip"
        ' we have a ZIP archive
    Case "rar"
        ' we have a RAR archive
    Case Else
        ' ignore
End Select
  

有没有办法让脚本打开.zip和.rar文件获取最新文件并在这个文件上运行tail.exe文件?

是的,就像你手动操作一样:

  • 使用WSHShell.Exec
  • 的归档工具的命令行版本
  • 将存档解压缩到临时目录(使用GetSpecialFolder()GetTempName()
  • 找出最新的文件
  • 在其上运行tail.exe,编写您的日志文件
  • 删除临时目录

答案 1 :(得分:0)

@Tomalak

你能看到这个问题吗?在日志文件中,它写了无效字符

我们决定忽略ZIP和RAR文件,而不是打开它们。

<强> CODE

`ext = LCase(FSO.GetExtensionName(sNewestFile))

    Select Case ext
        Case "log", "txt":
            if(STATUS = "ERROR") then
                'Runs the tail.exe file to get the last 10 lines of text in the [sNewestFile] and insert them to the log file.
                'This will only be done IF there is a status = "ERROR"
                errorStr = WScript.CreateObject("WScript.Shell").Exec( _ "tail -n 10 """ & sNewestFile & """" _ ).StdOut.ReadAll 
                objLogFile.writeline "" & vbCrLf
                objLogFile.writeline "Error Message Start" & vbCrLf
                objLogFile.writeline "" & errorStr & vbCrLf
                objLogFile.writeline "Error Message End" 
            End if
        Case "zip":
            if(STATUS = "ERROR") then
                objLogFile.writeline "" & vbCrLf
                objLogFile.writeline "This is a ERROR in the ZIP file" & vbCrLf
            End if 

        Case "rar":
            if(STATUS = "ERROR") then
                objLogFile.writeline "" & vbCrLf
                objLogFile.writeline "This is a ERROR in the RAR file" & vbCrLf
            End if             
    End Select`