Solved

vbscript - scheduled task Win XP

Posted on 2013-01-29
8
472 Views
Last Modified: 2013-02-06
I need a mechanism in VBscript that will stop the script at midnight. I can let scheduled tasks restart the script every morning. I'm using the date as part of an output/append file and need to start a new file each day.

Or, I can let the script loop differently and depend on the date change at midnight to result in a new output file.

And, if there is any kind of error, I need to make sure the script is restarted.

I'm polling a phone system to pick call data.

' Constants: Connection states
Const asCONN_DISCONNECTED=1
Const asCONN_LISTENING=2
Const asCONN_CONNECTED=3

' Constants: Error codes
Const asERR_SUCCESS=0

' Constants: Protocols
Const asPROTOCOL_RAW=1
Const asPROTOCOL_TELNET=2

' Constants output file
'Const asOutfile = "calldata"
Const asExtension = ".txt"
Dim Datenow : Datenow=date()
Dim vardate : vardate = Year(Datenow) & Month(Datenow) & Day(Datenow)
Dim asFileDate : asFileDate = vardate
'const asOutfile = "calldata" & vardate & asExtension
Dim ForAppending : ForAppending = 8
Dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Create a socket instance
Set asObj = CreateObject("ActivXperts.Socket") 
asObj.Protocol = asPROTOCOL_RAW

' Write some information to console
WScript.Echo "ActivSocket " & asObj.Version & " demo."

' Make a connection to port 1500 on remote server
asObj.Connect "192.168.1.181", 59003
WScript.Echo "Connect: result = " & asObj.LastError
If asObj.LastError = 11001 Then
    WScript.Echo "Error 11001: Specify a valid hostname-parameter in the Connect-method."
End If
If asObj.LastError = asERR_SUCCESS Then

  ' YES, we established a connection

  'WScript.Echo "Connection established" & vbCrLf

  ' Send some strings now

'WScript.Echo asCONNN_CONNECTED

    Do While asObj.ConnectionState = asCONN_CONNECTED
      If asObj.HasData Then
        str = asObj.ReceiveString
        'WScript.Echo "Received: " & str
    outFile=asOutfile & asFiledate & asextension
    Set objFile = objFSO.OpenTextFile("calldata" & vardate & ".txt",8,True)
    objFile.Write str & vbCrLf
    objFile.Close
        asObj.Sleep 100
      End If
    Loop
' And finally, disconnect
 asObj.Disconnect
Else
WScript.Echo "bad connection. You have to restart the script"
End If

Open in new window

0
Comment
Question by:lantervj
  • 5
  • 3
8 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38833703
Why not just let it run, and define the file name each time you write to it getting the current date each time?  Here's a rework that should do that, so it can just keep writing over midnight.  I also suspect you want to zero pad the month and day when they are single digits, so I added that.

' Constants: Connection states
Const asCONN_DISCONNECTED=1
Const asCONN_LISTENING=2
Const asCONN_CONNECTED=3

' Constants: Error codes
Const asERR_SUCCESS=0

' Constants: Protocols
Const asPROTOCOL_RAW=1
Const asPROTOCOL_TELNET=2

' Create a socket instance
Set asObj = CreateObject("ActivXperts.Socket") 
asObj.Protocol = asPROTOCOL_RAW

' Write some information to console
WScript.Echo "ActivSocket " & asObj.Version & " demo."

' Make a connection to port 1500 on remote server
asObj.Connect "192.168.1.181", 59003
WScript.Echo "Connect: result = " & asObj.LastError
If asObj.LastError = 11001 Then
    WScript.Echo "Error 11001: Specify a valid hostname-parameter in the Connect-method."
End If
If asObj.LastError = asERR_SUCCESS Then

  ' YES, we established a connection

  'WScript.Echo "Connection established" & vbCrLf

  ' Send some strings now

'WScript.Echo asCONNN_CONNECTED

    Do While asObj.ConnectionState = asCONN_CONNECTED
        If asObj.HasData Then
            WriteData asObj.ReceiveString
            asObj.Sleep 100
        End If
    Loop
' And finally, disconnect
    asObj.Disconnect
Else
    WScript.Echo "bad connection. You have to restart the script"
End If

Sub WriteData(sData)
    Const ForAppending = 8
    Const OutputFile = "calldata_$DATE$.txt"

    Dim DateNow
    Dim varDate
    Dim objFile
    Dim objFSO

    Datenow = Date()
    varDate = Year(DateNow) & Right("0" & Month(DateNow), 2) & Right("0" & Day(DateNow), 2)

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(Replace(OutputFile, "$DATE$", varDate), ForAppending, True)
    objFile.WriteLine sData
    objFile.Close

    Set objFile = Nothing
    Set objFSO = Nothing
End Sub

Open in new window

~bp
0
 

Author Comment

by:lantervj
ID: 38833815
Before I test this, which looks a great deal cleaner, can you explain sData on line 62?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38833825
Sure, sData is the parameter that the WriteData subroutine accepts.  It will be written to the output file.

Notice the following on line 38, it will pass the receive string to the routine to be written to the file.

WriteData asObj.ReceiveString

~bp
0
 

Author Comment

by:lantervj
ID: 38833832
I should have seen that.  Anyway, it works great.  How can I detect that the script is actually running or not?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:lantervj
ID: 38833910
If I schedule the script to run every hour, will windows see that it is already running and skip the task?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38834915
I thought you wanted this running constantly, not just once an hour?

Can you explain a bit about what it does in concept, and how the files it writes are used?

No, task scheduler will start multiple copies, so you would have to add code to the script to see if another copy of it is already running, and if so quit.

~bp
0
 

Author Comment

by:lantervj
ID: 38837044
I'm collecting raw data from a digital phone system and saving/appending it to a text file.  The file name has today's date in it so I have one file per day. The task can just sit there and run forever, but we both know that won't happen.  So, If the task is not running, I need to start it.  I have a scheduled task that runs every morning at 6AM to run the script.  If, for whatever reason, the computer goes down, I need to know that the task is not running (when the computer is back on) and start it back up.
0
 

Author Closing Comment

by:lantervj
ID: 38862678
Good start
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now