• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

Script to read text file

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
TIgerV
Asked:
TIgerV
  • 2
2 Solutions
 
Meir RivkinFull stack Software EngineerCommented:
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
 
Bill PrewCommented:
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
 
TIgerVAuthor Commented:
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
 
Bill PrewCommented:
Welcome, glad that was useful, thanks for the feedback.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now