Solved

vbscript - scheduled task Win XP

Posted on 2013-01-29
8
480 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 52

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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

21 Experts available now in Live!

Get 1:1 Help Now