Learn how to a build a cloud-first strategyRegister Now


Script to read text file

Posted on 2013-01-29
Medium Priority
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

by:Meir Rivkin
Meir Rivkin earned 1000 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 wscript.shell
		$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 59

Accepted Solution

Bill Prew earned 1000 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 59

Expert Comment

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


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 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