Script to read text file

Posted on 2013-01-29
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?
Question by:TIgerV
  • 2
LVL 42

Assisted Solution

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.


#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
		$b = $a.popup('WEATHER UPDATE FAILED',0,'weatherScript',0)

	#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"
		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){
		"register" {

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

Open in new window

LVL 52

Accepted Solution

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

' 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) & "]"
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
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


Author Closing Comment

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.
LVL 52

Expert Comment

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


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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

22 Experts available now in Live!

Get 1:1 Help Now