?
Solved

vbscript - scheduled task Win XP

Posted on 2013-01-29
8
Medium Priority
?
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 57

Accepted Solution

by:
Bill Prew earned 1500 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 57

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 57

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 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