ZenKensei Posted November 26, 2004 Share Posted November 26, 2004 Group, Looking for a little help with StringInStr. I currently run a report that checks the uptime of the servers in my domain. I then have an Autoit script that formats that text file into a CSV text file and then convert it to an HTML file for export to a web page. The problem I'm running into is that if a particular server is unreachable for any reason my text output is skewed for that server. See the example text from a typical report file:Server1 , 28 day(s), 2 hour(s), 19 minute(s), 34 second(s)UPTIME was unable to connect to host: Server2 In the above example of text output, the first line is correct with each column being seperated by the comma. The second line is where I'm having issues. The HTML template I'm using looks for 5 columns. It sees the second line of text above as being only one column and does a 'carry over' of the days, hours, minutes and seconds from the line above. The HTML output then appears as:Server1 , 28 day(s), 2 hour(s), 19 minute(s), 34 second(s)UPTIME was unable to connect to host: Server2, 28 day(s), 2 hour(s), 19 minute(s), 34 second(s)Even though the second server is down and has no reportable uptime it shows whatever the uptime was for the previous server in the list. What I need is a way to key on the 'Uptime was unable to connect to host: Server 2' and then append a 'N/A' to the next 4 columns so it appears in the text file as.UPTIME was unable to connect to host: Server2, N/A, N/A, N/A, N/AI assumed I could use StringInStr, but I have no idea how to append to the same line as the string I'm keying on, and since the line is dynamic as far as the server name goes, how to keep that server name in the text file.Any help would be appreciated.Thanks,ZK Link to comment Share on other sites More sharing options...
Developers Jos Posted November 26, 2004 Developers Share Posted November 26, 2004 are you resetting your variables everytime your read a new record? This will avoid carrying values over.... SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
ZenKensei Posted November 26, 2004 Author Share Posted November 26, 2004 JdeB - thanks for the quick reply.No, there are really no variables being set. The current text is simply being piped out by a program called Uptime.exe. It's raw output is simply one line similar to the following for a server it can reach:\\Server1 has been up for: 17 day(s), 1 hour(s), 41 minute(s), 0 second(s)and for a server it cannot reach is:UPTIME was unable to connect to host: Server1So from a batch file I'm piping the uptime from all the servers into a single text file like this:Uptime.exe \\Server1>>C:\Uptime.txtOnly I do this for all of the servers so the text file contains multiple lines, one for each server. I then use an Autoit script to pretty things up and replace a few words and punctuation marks so that the file has the appropriate CSV formatting for the columns I want. I think I see what your asking, and your idea. Just not sure how to establish the variable and then read each line and compare it. Such as Day(s)=0 and then change it based on what is in that 'column' or StringInStr section of each line, since the line in question has no day(s), hours(s), minute(s) or second(s) in it as a raw text file. Hmmm... Ok, if I serach each line for the word 'Uptime', then take everything to the right of the : (which is the server name that I want to keep) and then rewrite that line so that: UPTIME was unable to connect to host: Server1becomesServer1: 0 day(s), 0 hour(s), 0 minute(s), 0 second(s)The template used in the CSV2HTML program would see the entire 5 columns filled with data and would not try to 'carry over' data from the previous line. Ok, I understand the concept, but not the process. How can I rewrite the entire line once I determine that a line needs to be changed. I've used StringInStr to replace words, etc. in a single line, but never the entire line before. If you can provide a quick code sample I would greatly appreciate it.And thanks again for taken the time to respond so quickly before.ZK Link to comment Share on other sites More sharing options...
Developers Jos Posted November 26, 2004 Developers Share Posted November 26, 2004 (edited) Can you post the script & text file so we can have a look ? Or just PM it to me, would be happy to have a look... EDIT: after rereading i think this should do it: ; assume $irec is the input record $irec = "UPTIME was unable to connect to host: Server1" If StringInStr($irec, "was unable to connect to host") Then $irec = StringTrimLeft($irec, StringInStr($irec, ":")) $irec = $irec & ": 0 day(s), 0 hour(s), 0 minute(s), 0 second(s)" EndIf MsgBox(4096, 'debug:', '$irec:' & $irec);### Debug MSGBOX Edited November 26, 2004 by JdeB SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
ZenKensei Posted November 26, 2004 Author Share Posted November 26, 2004 JdeB, Getting close, your code works as long as you can specify the server name. However, the file may contain 30 lines with with 30 different servers. I want to replace those lines that are similar to the following:UPTIME was unable to connect to host: Server2 (Server2 is whatever server is on that line)withServer2, 0 day(s), 0 hour(s), 0 minute(s), 0 second(s)while keeping (but slightly changing) any line that may look like the following:\\Server1 has been up for: 28 day(s), 2 hour(s), 19 minute(s), 34 second(s)toServer1, 28 day(s), 2 hour(s), 19 minute(s), 34 seconds(s)An example of the shortend text file (uptime.txt) before the script is run is:\\Server1 has been up for: 28 day(s), 2 hour(s), 19 minute(s), 34 second(s)UPTIME was unable to connect to host: Server2afterward the script is run, I want the text file (uptime.txt) to look like:Server1, 28 day(s), 2 hour(s), 19 minute(s), 34 second(s)Server2, 0 day(s), 0 hour(s), 0 minute(s), 0 second(s)Here is the script I've been working with, using your sample code, but I'm having problem keeping all of the data in the text file (uptime.txt).$szReport = "Uptime.txt" $szText = FileRead($szReport, FileGetSize($szReport)) $irec = "UPTIME was unable to connect to host:" If StringInStr($szText, $irec) Then $irec = StringTrimLeft($irec, StringInStr($irec, ":")) $irec = $irec & ": 0 day(s), 0 hour(s), 0 minute(s), 0 second(s)" MsgBox(4096, 'debug:', '$irec:' & $irec);### Debug MSGBOX $szText = $irec Else $szText = StringReplace($szText, "\\", "") $szText = StringReplace($szText, "has been up for:", ",") FileDelete($szReport) FileWrite($szReport, $szText) EndIfAny suggestions would be appreciated.Thanks,ZKP.S. I downloaded Scite4Autoit3 after seeing the link on your response and all I can say is "WOW"!!! Good job on that, I started using it right away, especially the integrated Tidy tool Link to comment Share on other sites More sharing options...
Developers Jos Posted November 26, 2004 Developers Share Posted November 26, 2004 JdeB, Getting close, your code works as long as you can specify the server name. However, the file may contain 30 lines with with 30 different servers. <{POST_SNAPBACK}>You are reading the whole file into a variable. you really want the loop and read 1 record at the time with FileReadLine() .Just check the example given for FileReadLine() in the helpfile... SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
dwelcher Posted November 27, 2004 Share Posted November 27, 2004 Do you think this might work? This parses the Uptime.txt file and replaces it with a new one. It should be pretty easy to follow along, I hope. I'm sure you will have to adapt it to your situation. You will probably have to debug a little, too. expandcollapse popup$Directory = 'c:\Directory\' $LineNo = 0 $Pos = 0 $Line = '' $UptimeFile = FileOpen( $Directory & "Uptime.txt", 0) If $UptimeFile = -1 Then MsgBox(0, "Error", "Unable to open file - Uptime.txt" & ".")) Exit EndIf $Uptime2File = FileOpen( $Directory & "Uptime2.txt", 2) If $Uptime2File = -1 Then MsgBox(0, "Error", "Unable to open file - Uptime2.txt" & ".")) Exit EndIf ; Read in lines of Uptime.txt until the EOF is reached While 1 $LineNo = $LineNo + 1 $line = FileReadLine($UptimeFile) If @error = -1 Then ExitLoop EndIf $Pos = StringInStr( $line, 'unable' ) If $Pos > 0 Then $Line = 'Server' & $LineNo & ', 0 day(s), 0 hour(s), 0 minute(s), 0 second(s)' $Pos = 0 Else $RestOfLine = StringTrimLeft( $Line, $Pos + 1 ) $Line = 'Server' & $LineNo & ',' & $RestOfLine ) EndIf FileWriteLine( $Uptime2File, $Line & @CRLF ) WEnd FileClose( $Uptime ) FileClose( $Uptime2 ) FileDelete( $Directory & 'Uptime.txt' FileCopy( $Directory & 'Uptime2.txt', $Directory & 'Uptime.txt' ) FileDelete( $Directory & 'Uptime2.txt' Take care, -Dw --- A man has reached old age when he is cautioned to slow down by his doctor instead of by the police. Link to comment Share on other sites More sharing options...
ZenKensei Posted November 27, 2004 Author Share Posted November 27, 2004 JdeB & dwelcher, First, thanks for all your help with my post. For the most part I have it working due to the suggestions and code samples you both supplied. However, I've run into something within the code that doesn't appear to work and I'm curious as to why (it's not part of the main code, only the clean-up section so I can live without it). Below is the code I'm using, everything works except the commands following the WEnd statement. See Below: expandcollapse popup$clear = "0 day(s), 0 hour(s), 0 minute(s), 0 second(s)"; ensures data for all columns $keytxt = "UPTIME was unable to connect to host:"; establishes a check key $uptimefile = FileOpen("uptime.txt", 0) If $uptimefile = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $tempfile = FileOpen("uptemp.txt", 2) If $tempfile = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $line = FileReadLine($uptimefile) If @error = -1 Then Exit $uprecord = StringTrimRight($line, 13); Checks to match the $keytxt variable If $keytxt = $uprecord Then; compares for match of "UPTIME was unable to connect to host:" $server = StringTrimLeft($line, 38); sets variable to the server that is unreachable ; MsgBox(0, "$server", "variable is: " & $server) $line = StringReplace($line, $server, $clear) $line = StringReplace($line, $uprecord, $server & ",") ; MsgBox(0, "Line Test", "Line now reads: " & $line) FileWriteLine($tempfile, $line) Else $line = StringReplace($line, "\\", "") $line = StringReplace($line, " has been up for:", ",") FileWriteLine($tempfile, $line) EndIf Wend ; Clean-up section FileClose($uptimefile) FileClose($tempfile) FileDelete(@ScriptDir & '\Uptime.txt') FileCopy(@ScriptDir & '\uptemp.txt', @ScriptDir & '\Uptime.txt', 1) FileDelete(@ScriptDir & '\uptemp.txt') None of the 'file' functions above seem to work. I've tried various combinations of syntax (using the full file paths, etc.) but nothing seems to happen. The Uptime.txt and uptemp.txt files do not delete (and the filecopy function does not replace the uptime.txt file). Am I missing something? (simple I'm sure...) or is it due to the flow of the script? Thanks, ZK Link to comment Share on other sites More sharing options...
Developers Jos Posted November 27, 2004 Developers Share Posted November 27, 2004 None of the 'file' functions above seem to work. I've tried various combinations of syntax (using the full file paths, etc.) but nothing seems to happen. The Uptime.txt and uptemp.txt files do not delete (and the filecopy function does not replace the uptime.txt file). Am I missing something? (simple I'm sure...) or is it due to the flow of the script?Thanks,ZK<{POST_SNAPBACK}>Is the uptime.txt located in the scriptdirectory ? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
ZenKensei Posted November 27, 2004 Author Share Posted November 27, 2004 Uptime.txtJdeB, Yes, for writing and testing purposes I have all files (text and script) in the same directory. I have tried expanding the paths out to a fully qualified path (i.e. FileDelete ('C:\Autoit\uptime.txt') )but there appears to be no difference in the functionality. I'm attaching a copy of the Uptime.txt file that the script file runs against. It should create a file called "uptemp.txt" (that part works correctly). Just curious as to why the file functions don't...Thanks,ZK Link to comment Share on other sites More sharing options...
Developers Jos Posted November 27, 2004 Developers Share Posted November 27, 2004 Uptime.txtJdeB, Yes, for writing and testing purposes I have all files (text and script) in the same directory. I have tried expanding the paths out to a fully qualified path (i.e. FileDelete ('C:\Autoit\uptime.txt') )but there appears to be no difference in the functionality. I'm attaching a copy of the Uptime.txt file that the script file runs against. It should create a file called "uptemp.txt" (that part works correctly). Just curious as to why the file functions don't...Thanks,ZK<{POST_SNAPBACK}> just change this: If @error = -1 Then Exitto: If @error = -1 Then ExitLoop SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
ZenKensei Posted November 27, 2004 Author Share Posted November 27, 2004 DOH!!!!! Ok, I feel SOOOO stupid now (if I were Catholic I probably would say 3 'hail Mary's' or something for my sins....) That obviously did the trick! Thanks again for your help and patience, ZK P.S. and again for the Scite4Autoit3, I installed it yesterday and haven't stopped using it since.... Still exploring the nuances of all the tools, but the syntax checker and tidy tools are already irreplaceable. Link to comment Share on other sites More sharing options...
Developers Jos Posted November 27, 2004 Developers Share Posted November 27, 2004 P.S. and again for the Scite4Autoit3, I installed it yesterday and haven't stopped using it since.... Still exploring the nuances of all the tools, but the syntax checker and tidy tools are already irreplaceable.<{POST_SNAPBACK}>Glad you like it... Syntax checker is made by Tylo .. i did Tidy (made in AutoIt3) SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now