[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register 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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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 57

Expert Comment

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


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

649 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