1

My VBScript checks if the timestamp of a certain file is older then what has been passed in a argument.

If it is older then the argument allows, then the status of the log file created by this script is set to STATUS="ERROR". When STATUS="ERROR" then the tail of the error log (10 lines of code) is to be written to the Log file. So far, this works.

The problem is now that in the folder, where the scripts is running on, there are more then just .log and .txt files. One can also come across .zip or .rar files.

If one of them throws an error, the script when running tail on it gives the following

œš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Ò«

this is of course not what I would like to see.

The questions are:

  • Is there a way to make the script ignore other files extension than .log and .txt files and then just when the file extension is something else just inserts a string message?

  • Is there a way to make the script open the .zip and .rar file take the newest file inside and run the tail.exe file on this file?

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

Notes:

  • I got help in here for that solution: https://stackoverflow.com/a/32352356/3430698
  • The two variable are sNewestFile and sOldestFile that contains the newest and oldest file. They contain the entire path to the files with extension sNewestFile = oFile.Path
  • I have a filespec variable that is passed in as a argument that is the file extension. So I have tried to run a if sentence around that code above that checks if the status="ERROR", the if sentence was to check

    if (filespec <> ".txt" or file <> ".log") then 
        writeline "something" 
    else 
        'run the tail on the file 
    
3
  • I have a filespec variable that is passed in as a argument that is the file extension. So I have tried to run a if sentence around that code above that checks if the status="ERROR", the if sentence was to check if(filespec <> ".txt" or file <> ".log" then writeline "something" else 'run the tail on the file Commented Sep 4, 2015 at 11:20
  • perhaps the solution lies in, they use of: GetExtensionName(strPath) - Returns a string referring to the extension of the file. Ext From : ss64.com/vb/filesystemobject.html Commented Sep 4, 2015 at 11:25
  • Or perhaps even : stackoverflow.com/a/18921133/3430698 Commented Sep 4, 2015 at 11:28

2 Answers 2

4

Is there a way to make the script ignore other files extension than .log and .txt files

Yes. There is the FileSystemObject's 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

Is there a way to make the script open the .zip and .rar file take the newest file inside and run the tail.exe file on this file?

Yes, just the same way you would do it manually:

  • use the command line versions of the archive tools with WSHShell.Exec
  • unpack the archive into a temporary directory (use GetSpecialFolder() and GetTempName())
  • figure out the newest file
  • run tail.exe on it, write your log file
  • delete the temporary directory
Sign up to request clarification or add additional context in comments.

2 Comments

Is ext = LCase(FSO.GetExtensionName(file)) "file"is that the filename or the filepath?
It does not matter. GetExtensionName can handle both.
0

@Tomalak

Can you see something wrong with this ? in the log file it wrote Invalid Character

We decided that it should just ignorer the ZIP and RAR files instead of opening them.

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`

2 Comments

I would pull the If STATUS = "ERROR" ... (not that parentheses are generally not necessary in If) outside of the Case, since you only ever seem to want to do anything when there is an error.
That being said, StackOverflow is not a forum. Do not post follow-up posts. They do not work here, because a) answers are not being ordered by date (but by number of votes), b) the section below the question is reserved for answers (and though you can answer your own question, this post does not qualify as an answer) and c) we do not like seeing a thread mutate into an interactive code review/debugging session, because that tends to be completely useless for future visitors of the thread. So, for the future, please keep to the "question at the top, answer at the bottom, no followups" style.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.