none
Help needed: VBS scripts to copy out logs files between time RRS feed

  • Question

  • I am new to Vb scripts. i would like to seek help on how to create a scripts to copy files between time.

    example:

    I got log files in a folder which will keep logging from time to time.

    i wish to copy out the logs file which 1 can set then begin and end time so the scripts able to help me copy out all the realated log files in correct time frame.

    Please kindly help here. Thanks.

    Thursday, March 17, 2011 8:02 AM

Answers

  • Here you go:

    sTextToFind = "FINISH"
    sTimeStamp = "TimeStamp"
    sResult = "Nothing found"
    bNear = False
    bFound = False

    Do Until objFile.AtEndOfStream
        sText = objFile.ReadLine
        If bNear And InStr(1, sText, sTimeStamp, 1) Then
            bFound = True
            sText = Replace(sText, ">", "<")
            sResult = Split(sText, "<")(2)
            Exit Do
        End If

        If InStr(1, sText, sTextToFind, 1) > 0 Then bNear = True
    Loop

    WScript.Echo sResult

    Friday, March 18, 2011 3:37 PM

All replies

  • For a beginner in VB Scripting the usual process goes like this:
    1. Download the help file script56.chm from the Microsoft site. It explains every VB Script function and method and contains numerous examples.
    2. Check the code examples in this forum for further hints on how to write VB Script code.
    3. Write a program that more or less does what you want it to do.
    4. Ask here for advice when you get stuck.
    If you adopt this approach then you will get as much assistance in your scripting efforts as you need.
    Thursday, March 17, 2011 8:08 AM
  • i have the below code is working to copy files. however, i still need to do modification on date format. i cant get it. i will need to get the start and end date from another file tat the format of the date is "2011-03-10T00:37:37.3632515+08:00", how can i make this format to be enter into the below scripts (format --> 03/10/2011 00:37?)??? kindly help on this. Thanks.
    Dim dtmStart, dtmEnd 
    dtmStart = InputBox("Enter start time: ") 
    dtmEnd = InputBox("Enter end time: ") 
    dtmStart = CDate(dtmStart) 
    dtmEnd = CDate(dtmEnd) 
    
    strFolder = "C:\Documents and Settings\m\Desktop\Copy log file\logfile" 
    Set objFSO=CreateObject("Scripting.FileSystemObject") 
    Set objFolder = objFSO.GetFolder(strFolder) 
    strDirectory = "C:\Documents and Settings\m\Desktop\Copy log file" 
    'Set objFolderC = objFSO.CreateFolder(strDirectory) 
    For Each objFile in objFolder.Files 
     dtmFileDate = objFile.DateLastModified 
     If dtmFileDate > dtmStart And dtmFileDate < dtmEnd Then 
      objFile.Copy "C:\Documents and Settings\m\Desktop\Copy log file\LogPack\", True 
     Else 
     End If 
    Next 
    
    
    Thursday, March 17, 2011 2:50 PM
  • You ask the user to enter the start and end time. What will you assume for the start and end dates?

    Your code uses these assignments:
    strFolder = "C:\Documents and Settings\m\Desktop\Copy log file\logfile"
    strDirectory = "C:\Documents and Settings\m\Desktop\Copy log file"

    Since the terms "Folder" and "Directory" are more or less synonymous, your choice of variable names is confusing and makes maintenance difficult. I suggest you make it like so: 

    strFolder = "C:\Documents and Settings\m\Desktop\Copy log file\logfile"
    strTarget = "C:\Documents and Settings\m\Desktop\Copy log file\LogPack\"

    Note also that the lines

     If dtmFileDate > dtmStart And dtmFileDate < dtmEnd Then
      objFile.Copy "C:\Documents and Settings\m\Desktop\Copy log file\LogPack\", True
     Else
     End If 

    can be expressed much more clearly and concisely like so:

     If dtmFileDate > dtmStart And dtmFileDate < dtmEnd Then objFile.Copy strTarget, True

    This, of course, is a matter of preference.
    Thursday, March 17, 2011 5:12 PM
  • Thanks.... it helps to prevent confusion. :)

    Actually the START and END date format that i would like to be is in this format  "03/10/2011 00:37".

    The start and end date is actually extracted from a xml file. but i duno how to get this out from the files.

    below is part of the line quoted. what i need is the time "2011-03-10T00:37:37.3632515+08:00". what i can think of is to read the file until i get the line (<Operation type="FINISH">) then go to next 2 line to get the date. However, i not sure how this can be done. please kindly help.

       </Operation>
       <Operation type="FINISH">
        <Column name="Size">1749</Column>
        <Column name="TimeStamp">2011-03-10T00:37:37.3632515+08:00</Column>
        <Column name="Operator">mzulaliz</Column>
        <Column name="Description"><![CDATA[Done]]></Column>
        <Column name="Status">Success</Column>
       </Operation>

    Below is the script i try out...but not working. :(

    TextToFind = "FINISH"
    TimeStamp = "TimeStamp"
    i = 1
    Do Until objFile.AtEndOfStream
      stext = objFile.ReadLine
    	StringPosition = InStr(stext, TextToFind)
        'InStr will return the position of the "found string" else it will return 0
        If StringPosition = 0 Then
          Wscript.Echo "Nothing Found"
        Else
        Cells(i, 1) = Mid(stext, LenghtOfTextToFind + StringPosition, 13)
          i = i + 1
      End If
    Loop
    Friday, March 18, 2011 3:16 PM
  • Here you go:

    sTextToFind = "FINISH"
    sTimeStamp = "TimeStamp"
    sResult = "Nothing found"
    bNear = False
    bFound = False

    Do Until objFile.AtEndOfStream
        sText = objFile.ReadLine
        If bNear And InStr(1, sText, sTimeStamp, 1) Then
            bFound = True
            sText = Replace(sText, ">", "<")
            sResult = Split(sText, "<")(2)
            Exit Do
        End If

        If InStr(1, sText, sTextToFind, 1) > 0 Then bNear = True
    Loop

    WScript.Echo sResult

    Friday, March 18, 2011 3:37 PM