Solved

vbscript - scheduled task Win XP

Posted on 2013-01-29
8
492 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 53

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 53

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 53

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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