Page 1 of 1 1
Topic Options
#189295 - 2008-08-27 01:49 PM Read Content from .log- file
Schwinni Offline
Fresh Scripter

Registered: 2008-06-09
Posts: 8
Hello everybody,

is there a possibility to let kix check the content of a file?

I would like to use kix to open a created *.log- file (textfile) and check the content if the phrase "Skipped with error" is included.
It is always this sentence, if errors occur!
If yes, kix should send a mail with "error" in subject line, else send mail with "success" in Subject line.

Thanks for your help
Michael

Top
#189296 - 2008-08-27 02:17 PM Re: Read Content from .log- file [Re: Schwinni]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 687
Loc: Maryland, USA
Sure,

Check out the manual for Open and InStr. Rough example might be:

 Code:
If Open(1,"my.log") = 0
   $strLine = ReadLine(1)
   While @ERROR = 0
      If Instr($strLine,"Skipped with error") <> 0
         ? "Found it!"
      EndIf
      $strLine = ReadLine(1)
   Loop
EndIf


Regards,

Brad V

Top
#189297 - 2008-08-27 04:20 PM Re: Read Content from .log- file [Re: BradV]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
To expand on Brad's thoughts..

Brad's example outputs a message each time a string is found. To break out of the loop the first time you find the desired string:
 Code:
  $Tag = 1
  $strLine = ReadLine(1)
  While $Tag And Not @ERROR
    If InStr($strLine, 'Skipped with error')
      $Tag = 0
    EndIf
    $strLine = ReadLine(1)
  Loop
  $ = Close(1)
  If $Tag = 0 ; string was found!
    ; use Blat to send a Success email
  Else
    ; use Blat to send an Error email
  EndIf

There are several examples of using Blat on KORG, so I won't repeat that here.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#189299 - 2008-08-27 05:10 PM Re: Read Content from .log- file [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
To contract on Glenn's thoughts ;\)
 Code:
$strLine = ReadLine(1)
While Not $Tag And Not @ERROR
	$Tag=InStr($strLine, 'Skipped with error')
	$strLine = ReadLine(1)
Loop
$=Close(1)
If $Tag  ; string was found!
    ; use Blat to send a "skipped found" email
Else
    ; use Blat to send an "skipped not found" email
EndIf


Gotta save those CPU cycles.

Top
#189305 - 2008-08-27 06:57 PM Re: Read Content from .log- file [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
LOL - I thought about going back and doing some reverse logic, where a true value in Tag could break the loop AND cause an exit on one of multiple conditions, which would expand on the contracted expansion of the first example, but I was running late for the Kix scripting class I was teaching. Strangely enough, much of todays lesson was processing and breaking out of loops \:\)

G-
_________________________
Actually I am a Rocket Scientist! \:D

Top
#189876 - 2008-09-25 01:44 PM Re: Read Content from .log- file [Re: BradV]
Kaiminator Offline
Fresh Scripter

Registered: 2006-08-28
Posts: 17
 Code:
If Open(1,"my.log") = 0
   $strLine = ReadLine(1)
   While @ERROR = 0
      If Instr($strLine,"Skipped with error") <> 0
         ? "Found it!"
      EndIf
      $strLine = ReadLine(1)
   Loop
EndIf


this example works for me. But how is the best way to parse only the last found line in the log file? i dont need all lines, which the phrase is found, i only need the last line

thanx in advance


Edited by Kaiminator (2008-09-25 01:45 PM)

Top
#189877 - 2008-09-25 02:28 PM Re: Read Content from .log- file [Re: Kaiminator]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
 Code:
If Open(1,"my.log") = 0
   $strLine = ReadLine(1)
   While @ERROR = 0
      $strLine = ReadLine(1)
   Loop
EndIf

? "Last line in the file is "$strLine


Or if you need to manipulate the data in the file in any way..

 Code:
$strCount = 0
If Open(1,"my.log") = 0
   $strLine = ReadLine(1)
   While @ERROR = 0
      redim preserve $lineArray[$strCount]
      $LineArray[$strCount] = $strLine
      $strLine = ReadLine(1)
      $strCount = $strCount + 1
   Loop
EndIf

? "Last line in the file is "$LineArray[$strCount]
_________________________
Today is the tomorrow you worried about yesterday.

Top
#189878 - 2008-09-25 02:29 PM Re: Read Content from .log- file [Re: Kaiminator]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA


One way is to use Readfile(), found in the UDF forum, which would load your file into an array. It would look something like (don't forget to put the READFILE UDF to the bottom of your script)

untested
 Code:
$filearray=readfile("yourfile")
$lastline=$filearray[ubound($filearray)]

? $lastline


Of course you could always use the loop code you have and just use/keep the last value.
_________________________
(... better days ahead)

Top
#189880 - 2008-09-25 03:15 PM Re: Read Content from .log- file [Re: Allen]
Kaiminator Offline
Fresh Scripter

Registered: 2006-08-28
Posts: 17
thx. found this solution by myself. i keep the last value in the var
Top
#189954 - 2008-10-01 11:28 AM Re: Read Content from .log- file [Re: Kaiminator]
Kaiminator Offline
Fresh Scripter

Registered: 2006-08-28
Posts: 17
sorry, one more question. Dont know, if its better to open a new topic or not.

i write several values of variables to a textfile. this looks like this:

 Code:
username;path to myfiles;size;networkpath;size;date last backup


this script starts with every user logon in user context. So far - so good.

How can i manage this, that the script looks in the textfile for the username and overwrites this line with the new values?

Top
#189955 - 2008-10-01 11:42 AM Re: Read Content from .log- file [Re: Kaiminator]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You could use the ReadFile() UDF to read the entire file into an array, then do a check to see if there is a line with the username in the array, set the value of that specific array element to the values you want and write the entire array to a file.
Don’t forget to include the ReadFile() UDF in the code below.

Something like this (example below is untested!):
 Code:
Break on

$file = readfile("c:\file.log")

For $i = 0 To UBound($file)
	If InSTR($file[$i], @USERID)
		$file[$i] = "Some new text here"
	EndIf
Next

$rc = Open(1,"c:\newfile.log", 5)

For $i = 0 To UBound($file)
	$rc = WriteLine(1, $file[$i] + @CRLF)
Next

$rc = Close(1)
Move "c:\file.log" "c:\oldfile.log"
Move "c:\newfile.log" "c:\file.log" 
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#189957 - 2008-10-01 01:32 PM Re: Read Content from .log- file [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
This is an ideal application for an INI file, where you look for data based on a key. You can reduce the read/write to two lines of code:
 Code:
$Array = Split(ReadProfileString('MyData.ini', 'USERDATA', USERID), ';')

to read your ";" delimited string into an array, and
 Code:
$ = WriteProfileString('MyData.ini', 'USERDATA', @USERID, Join($Array, ';'))

to write the array back to a delimited string. The resulting file will look like this for user JSmith:
 Code:
[USERDATA]
JSmith=username;path to myfiles;size;networkpath;size;date last backup


Of course, you can extend this concept and write individual values by using the UserID as the section. To Write:
 Code:
$ = WriteProfileString('MyData.ini', @USERID, 'User', $UserName)
$ = WriteProfileString('MyData.ini', @USERID, 'Path', $Path2MyFiles)
$ = WriteProfileString('MyData.ini', @USERID, 'Size1', $DSize)
$ = WriteProfileString('MyData.ini', @USERID, 'NetPath', $NetPath)
$ = WriteProfileString('MyData.ini', @USERID, 'Size2', $NSize)
$ = WriteProfileString('MyData.ini', @USERID, 'Backup', $LastBackup)

which allows you to directly read the user's values with:
 Code:
$UserName = ReadProfileString('MyData.ini', @USERID, 'User')
$Path2MyFiles = ReadProfileString('MyData.ini', @USERID, 'User')
$DSize = ReadProfileString('MyData.ini', @USERID, 'User')
$NetPath = ReadProfileString('MyData.ini', @USERID, 'User')
$NSize = ReadProfileString('MyData.ini', @USERID, 'User')
$LastBackup = ReadProfileString('MyData.ini', @USERID, 'User')

In this manner, you don't need to deal with (or worry about mangling) the data from other users. Think of INI files as a simple database with records (sections) and fields (section values).

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#189966 - 2008-10-01 04:26 PM Re: Read Content from .log- file [Re: Glenn Barnas]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
You guys need to golf...
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#189968 - 2008-10-01 04:45 PM Re: Read Content from .log- file [Re: Björn]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: Björn
You guys need to golf...


LOL
There is a golf course next to my office if I look out the window so let’s have a round ;\)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.159 seconds in which 0.12 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org