Page 1 of 1 1
Topic Options
#176806 - 2007-06-07 03:55 PM Trouble creating script to search for text in file and insert new line
corey Offline
Fresh Scripter

Registered: 2006-05-09
Posts: 7
I am trying to search a text file for an item (@wksta). If a matching name is found, I want to exit the script. If the name is not found, I want to add the name (@wksta) to the text file.

I am fairly new to kixtart though I have been using it for a while now. I think I need to use instr() and writeline() but I have not been successful on getting this to work correctly.

Any thoughts? Does anyone have any similar code I could try manipulating to work?

Any help on this will be greatly appreciated.

Thank You

Top
#176807 - 2007-06-07 03:58 PM Re: Trouble creating script to search for text in file and insert new line [Re: corey]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
First question - is this "text file" formatted in any known way - like an INI format or XML format file ?

-Shawn

Top
#176808 - 2007-06-07 04:17 PM Re: Trouble creating script to search for text in file and insert new line [Re: corey]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Here's code to search trough the txt file for a specific word.
Script echo's a number for the times the word was found and 0 if it wasn't found at all.

 Code:
$Script = "C:\boot.ini"
$SearchWord = "windows"
$FSO = CreateObject("Scripting.FileSystemObject")
$ReadScript = $FSO.OpenTextFile($Script, 1, -1)
While Not $ReadScript.AtEndOfLine
  $var = InStr($ReadScript.ReadLine,$SearchWord)
  If $var <> 0
   $fnCount = $fnCount + 1  
  EndIf
Loop
$ReadScript.Close
$FSO = ""
? $fnCount

Top
#176809 - 2007-06-07 05:09 PM Re: Trouble creating script to search for text in file and insert new line [Re: Arend_]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: apronk
Script echo's a number for the times the word was found and 0 if it wasn't found at all.


There are a couple of issues with your search scriptlet which may or may not cause a problem depending on how the file is structured.

1. You are counting the number of lines that the word might appear on, *not* the number of times that the word appears. If the word appears twice on one line you will only count it once. Think about using Split() and Ubound() as a fast way of counting how many times a word appears in a string - you can then use .ReadAll instead of enumerating each line.

2. More relevant to this post, the InStr() will match substrings, so if the workstation is called "PC1" it will match "PC1", "PC13", "NotReallyPC1AtAll" and so on.

Top
#176811 - 2007-06-07 06:03 PM Re: Trouble creating script to search for text in file and insert new line [Re: Richard H.]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Richard: I totally agree with you, however this scriplet is not designed with a fullfledged search function in mind, just to quickly and nasty do what it is supposed to \:\)
Top
#176812 - 2007-06-07 07:20 PM Re: Trouble creating script to search for text in file and insert new line [Re: Arend_]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Arend. I enjoyed seeing how you coded this using FSO instead of the embedded Kixtart functions. Wonder what the "performance" is like - comparing the two approaches. Can't help but feel that maybe FSO is faster. You ever benchmark the two approaches ?
Top
#176813 - 2007-06-07 07:38 PM Re: Trouble creating script to search for text in file and insert new line [Re: Shawn]
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Readfile()
Ascan()
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#176814 - 2007-06-07 09:12 PM Re: Trouble creating script to search for text in file and insert new line [Re: Radimus]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
or

ReadAll()
Instr()

Top
#176821 - 2007-06-08 09:45 AM Re: Trouble creating script to search for text in file and insert new line [Re: Arend_]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: apronk
Richard: I totally agree with you, however this scriplet is not designed with a fullfledged search function in mind, just to quickly and nasty do what it is supposed to \:\)


Well, you can do it quicker, nastier and get more accurate results ;\)

 Code:
$sFile="test.txt"
$oFSO=CreateObject("Scripting.FileSystemObject")
$oFile=$oFSO.OpenTextFile($sFile, 1, -1)
 
$sFileData=@CRLF+$oFile.ReadAll
 
"File content:"
$sFileData+@CRLF
 
"The word 'foobar' appears "+UBound(Split($sFileData,"foobar"))+" times in the file"+@CRLF
"The word 'grape' appears "+UBound(Split($sFileData,"grape"))+" times in the file"+@CRLF
"The word 'grape' appears on its own on "+UBound(Split($sFileData,@CRLF+"grape"+@CRLF))+" lines in the file"+@CRLF
"The letter 'r' appears "+UBound(Split($sFileData,"r"))+" times in the file"+@CRLF
 
exit 0


When run with a small test file these are the results:
 Quote:
D:\temp>kix32 ml.kix
File content:
Orange
Lemon
Grapefruit
Melon
Grape

The word 'foobar' appears 0 times in the file
The word 'grape' appears 2 times in the file
The word 'grape' appears on its own on 1 lines in the file
The letter 'r' appears 4 times in the file

Top
#176830 - 2007-06-08 03:12 PM Re: Trouble creating script to search for text in file and insert new line [Re: Richard H.]
corey Offline
Fresh Scripter

Registered: 2006-05-09
Posts: 7
Thank you all for your help.

The file I am opening is a CSV file with no more than 200 lines but each line can be a little lengthy but not too bad.

In the end the simplists is the best, 'is the word there or not'. Once the word is found I don't need to look for the word any more? Since this is a 'workstation name' there should not be any duplicates (hopefully).

I will test out the above scripts and see what I come up with. If anyone has any more comments or samples, that would be great also.

The above code (first one) runs without any errors so I just need to modify it a little to get the yes/no result (hopefully I will be able to do).

Anyway thank you all again for your help.

Top
#176835 - 2007-06-08 04:24 PM Re: Trouble creating script to search for text in file and insert new line [Re: corey]
corey Offline
Fresh Scripter

Registered: 2006-05-09
Posts: 7
My previous e-mail was a bit early. I would expect the script to stop if $var<>0 but it keeps going (i added a goto 'end'. When debuging the script, it appears as if the $var= line is not read.

Any thoughts?

Top
#176836 - 2007-06-08 04:34 PM Re: Trouble creating script to search for text in file and insert new line [Re: corey]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Post your script and a sample of the CSV file (use CODE) tags when posting the code.

We'll have a much better idea of what the problem might be if we can see what you've got.

Top
#176839 - 2007-06-08 05:59 PM Re: Trouble creating script to search for text in file and insert new line [Re: Richard H.]
corey Offline
Fresh Scripter

Registered: 2006-05-09
Posts: 7
Basically, if the computer name is there or not there, I want to exit or write a line to a csv file.

This is my first attempt at adding code here. I am uncertain if I need [] brackets or not...we will see.

CODE
$Script = "$LogPath\CompHardware.csv"
$SearchWord = "C-205"
$FSO = CreateObject("Scripting.FileSystemObject")
$ReadScript = $FSO.OpenTextFile($Script, 1, -1)
While not $ReadScript.AtEndOfLine
$var = InSTR($ReadScript.ReadLine,$SearchWord)
If $var <> 0
GoTo "End"
EndIf
$RES=WRITELINE(5,$LogText+$RETURN)
Loop
$ReadScript.Close
$FSO = ""
? $fnCount
/CODE

Below is a sample of the CSV file.....
C-205,0015C5A97814,Intel Pentium Model 14,1664,Intel 11.1.0.86,Windows XP Professional,Service Pack 2

Reasons for using csv files is that I can quickly review all computers to see if something is outdated/missing or needs to be addressed on a single page and I can sort each column in Excel. I am looking to get into WMI someday.

Top
#176846 - 2007-06-08 08:18 PM Re: Trouble creating script to search for text in file and insert new line [Re: corey]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Yes you need to surround the code and /code tag with square brackets.

I modified your code a bit and made it use kix commands and function. FSO might work great but this should also. You might want to add + @crlf at the end of the line it writes to the log file.

 Code:
Break on

$searchword = "C-205"
$logfile = "\\server\share\comphardware.csv"

$rc = Open(1, $logfile, 5)
$line = ReadLine (1)

While @ERROR = 0
	If NOT InStr($line,$SearchWord)
		$rc = WriteLine(5,$LogText + $Return)
	EndIf
	$line = ReadLine (1)
Loop

$rc = Close (1)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#176877 - 2007-06-11 09:44 AM Re: Trouble creating script to search for text in file and insert new line [Re: corey]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
When you post your code look just above the top of the text box for the "#" button - click on this and it will insert code tags into the text box. Paste your code between the tags. You can also type them manually, once you know what they look like.

If your workstation names are always the first field in the CSV file, then this will work for you:
 Code:
$sCheckFile="CompHardware.csv"
$sExceptionFile="exceptions.csv"
 
$sTextToFind=@WKSTA
 
$oFSO=CreateObject("Scripting.FileSystemObject")
$oFile=$oFSO.OpenTextFile($sCheckFile, 1, -1)
$sFileData=@CRLF+$oFile.ReadAll
 
If InStr($sFileData,@CRLF+$sTextToFind+",")
	"Yes, '"+$sTextToFind+"' is in the file"+@CRLF
Else
	"No, '"+$sTextToFind+"' is not in the file"+@CRLF
	; Cheap and dirty hack to log exception
	$=RedirectOutput($sExceptionFile)
	$sTextToFind+@CRLF
	$=RedirectOutput("")
EndIF


This will write the missing workstation name to an exception file - of course it will do it every time that the script runs, so you might want to turn the code that checks if a string is in the file into a function and call it once for the main file, and again for the exception file before you write to it.

Note, don't use "GoTo" in your code. There are a very few exceptional occasions when it is the appropriate choice, but you are unlikely to encounter them. If you are using "GoTo" is usually means that you haven't thought about the code flow and decision paths.

Top
#176879 - 2007-06-11 10:45 AM Re: Trouble creating script to search for text in file and insert new line [Re: Shawn]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
 Originally Posted By: Shawn
Arend. I enjoyed seeing how you coded this using FSO instead of the embedded Kixtart functions. Wonder what the "performance" is like - comparing the two approaches. Can't help but feel that maybe FSO is faster. You ever benchmark the two approaches ?


Shawn, I never really benchmarked them, I just prefer that way of reading/writing to files. I "feel" it's a faster way as well \:\)
Should benchmark it sometimes.... anyway FSO is probably in my blood cos I went from vbs to kix a couple of years ago \:\)

Richard, nice adaptation \:\)

Top
#176886 - 2007-06-11 02:02 PM Re: Trouble creating script to search for text in file and insert new line [Re: Richard H.]
corey Offline
Fresh Scripter

Registered: 2006-05-09
Posts: 7
I did some testing and once I figured out what was happening, it worked great.
Other than a few minor changes it worked as given. Basically if the computer name exists, the code just does a RETURN to the previous script. If the name doesn't exist, the remainder of this script is run.

I am so happy that this works. I learned a little too which I guess isn't a bad thing.

I undertand now too that using GOTO is not a good thing always.

Thank You all for your help. I am working on the other code to see if I can get that to work they way I want also (just for my knowledge).

 Code:
 
$sTextToFind=@WKSTA
$oFSO=CreateObject("Scripting.FileSystemObject")
$oFile=$oFSO.OpenTextFile($logfile, 1, -1)
$sFileData=@CRLF+$oFile.ReadAll
 If InSTR($sFileData,@CRLF+$sTextToFind+",")
  "Yes, '"+$sTextToFind+"' is in the file"+@CRLF
   Close($logfile)
   Return
Else
  "No, '"+$sTextToFind+"' is not in the file"+@CRLF
 

Top
#176890 - 2007-06-11 03:15 PM Re: Trouble creating script to search for text in file and insert new line [Re: corey]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Don't forget to close the "If-Else" with "EndIf" ;\)
Top
#176891 - 2007-06-11 03:32 PM Re: Trouble creating script to search for text in file and insert new line [Re: Richard H.]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
 Originally Posted By: Richard H.
Well, you can do it quicker, nastier and get more accurate results ;\)


I agree ;\)
 Code:
$sFile="c:\boot.ini"
$oFSO=CreateObject("Scripting.FileSystemObject")
$oFile=$oFSO.OpenTextFile($sFile, 1, -1)
$sFileData=$oFile.ReadAll

? "The word 'professional' appears "+UBound(Split($sFileData,"professional"))+" times in the file"
? "The word 'xp' appears "+UBound(Split($sFileData,"xp"))+" times in the file"
? "The word 'timeout=30' appears on its own on "+UBound(Split($sFileData,@CRLF+"timeout=30"+@CRLF))+" lines in the file"
? "The letter 'r' appears "+UBound(Split($sFileData,"r"))+" times in the file"

Top
Page 1 of 1 1


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

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

Generated in 0.18 seconds in which 0.114 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