#176806 - 2007-06-07 03:55 PM
Trouble creating script to search for text in file and insert new line
|
corey
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
|
|
|
|
#176808 - 2007-06-07 04:17 PM
Re: Trouble creating script to search for text in file and insert new line
[Re: corey]
|
Arend_
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.
$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
|
|
|
|
#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_
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
|
|
|
|
#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
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
|
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
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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 
$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:
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
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
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
|
|
|
|
#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
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
|
|
|
|
#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
   
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:
$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_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
|
|
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
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).
$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_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
Don't forget to close the "If-Else" with "EndIf"
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1619 anonymous users online.
|
|
|