Solved

Script to read text file

Posted on 2013-01-29
4
525 Views
Last Modified: 2013-01-29
I have a text file put out by my weather station that looks like this:
9:08a,01/29/13,54.8,55.0,48.7, 96,29.856,Falling Slowly,11.3,WSW
(Yes, there is a space at the beginning, I don't know why.)

Text file is located in C:\weather\TIger.txt

I want to look at the date and time (first 2 fields), and if they are within the last 30 minutes, then look at the remaining fields.  If the time is not within 30 minutes, send text box "WEATHER UPDATE FAILED"

If field 5 is between 37 and -17, I want to run the file
"c:\program files\dta\api.exe scenario 20"
If field 5 is between -18 and -29, I want to run the file
"c:\program files\dta\api.exe scenario 21"

I will use task scheduler to run this script every 30 minutes.  In addition, I will probably add some other break points based on additional values, such as field 4 being >80, etc.

Is it even possible?
0
Comment
Question by:TIgerV
  • 2
4 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 250 total points
ID: 38831257
does the line of text is the first line in the file TIger.txt?


anyway, i gathered a powershell script which do what you want.
save the file as weatherScript.ps1 somewhere in your file system.
to run it the first time, (creates the task scheduler) :
powesehll weatherScript.ps1 "register"

from now on, the task scheduler will run the same script every 20 minutes, passing "monitor" parameter to the script.
this tells the script to do the text logic u required.
btw, in case you need to move the script location after running it the first time, you should delete the task scheduler beforehand.
or run the following line:
powesehll weatherScript.ps1 "unregister"

if you want to check the txt file in between time intervals of the scheduler you can do it by running this line:
powesehll weatherScript.ps1 "monitor"

in case you wish to add code to the script, check the $tokens variable.
it contains all the values located between the comma in the text line.
it based on 0 index not 1, so the 1st value is $tokens[0], 2nd value is $tokens[1] etc.

let me know if you got your hands on it.
cheers


cls

#run by task scheduler
function monitor{
	$txt = gc c:\temp\TIger.txt
	$tokens = $txt.split(',')

	#check datetime
	if((Get-Date).addminutes(-30) -gt [datetime]::Parse($tokens[1]+' '+$tokens[0]+'m')){\
		#dispaly popup message 
		$a = new-object -comobject wscript.shell
		$b = $a.popup('WEATHER UPDATE FAILED',0,'weatherScript',0)
		return
	}

	#check if value between 37 and -17, then run api.exe
	$num = $tokens[4]
	if($num -gt -17 -and $num -lt 37){
		& "c:\program files\dta\api.exe" "scenario", "20"
	}else
	{
		if($num -lt -18 -and $num -gt -29){
			& "c:\program files\dta\api.exe" "scenario", "21"
		}
	}
}

#first time run
function register{
	#full path of this script
	$scriptFullPath = $MyInvocation.MyCommand.Definition

	#cerate scheduler
	schtasks /create /sc minute /mo 30 /tn "WeatherTaskScript" /tr "$scriptFullPath monitor"
}

function unregister{
	schtasks /delete /f /tn "WeatherTaskScript"
}

if ($args.Length -eq 1){
	switch($args[0]){
		"register" {
			register
			return
		}
		"monitor"{
			monitor
			return
		}
		"unregister"{
			monitor
			return
		}
	}
}

Write-Host "weatherScript [register/unregister/monitor]"	

Open in new window

0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 38831441
Here's a quick VBS approach, let me know if you have any questions.  Adjust the file name as needed at the top.

'9:08a,01/29/13,54.8,55.0,48.7, 96,29.856,Falling Slowly,11.3,WSW

' Define any needed constants
Const DataFile = "C:\EE\EE28012495\weather\tiger.txt"
 
Const ForWriting = 2
Const ForReading = 1
 
' Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
 
' Open the text file - strData now contains the whole file
Set objFile = objFSO.OpenTextFile(DataFile, ForReading)
strData = objFile.ReadAll
objFile.close

' Split the text file into lines
arrLines = Split(strData, vbCrLf)

' Split the first line into fields
arrFields = Split(arrLines(0), ",")

' Make sure the date / time stamp is valid
If Not IsDate(arrFields(1) & " " & arrFields(0)) Then
   MsgBox "Invalid date/time found: [" & arrFields(1) & " " & arrFields(0) & "]"
   Wscript.Quit
End If

' Convert date / time strings to a date value
datStamp = CDate(arrFields(1) & " " & arrFields(0))

' See if it's been update in last 30 minutes, report error if not
If DateDiff("n", datStamp, Now) > 30 Then
   MsgBox "WEATHER UPDATE FAILED"
   Wscript.Quit
End If

' Check value of field 5 and act accordingly
dblField5 = CDbl(Trim(arrFileds(4)))
If dblField5 >= -17 And dblField5 <= 37 Then
   objShell.Run ("c:\program files\dta\api.exe scenario 20")
End If

If dblField5 >= -29 And dblField5 <= -18 Then
   objShell.Run ("c:\program files\dta\api.exe scenario 21")
End If

Open in new window

~bp
0
 

Author Closing Comment

by:TIgerV
ID: 38832272
I have to split the points on this one.  

While the first is probably works well, my security environment doesn't allow powershell scripts, so I was unable to test it.  I didn't know they had set a GPO until today!  

BP's solution worked (one spelling error on line 40), although it was not the first one in.  I awarded equal points since it is a VB script, and I posted in the VB script area.

Many thanks to you both.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38832646
Welcome, glad that was useful, thanks for the feedback.

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

19 Experts available now in Live!

Get 1:1 Help Now