bqbowden Posted March 6, 2011 Posted March 6, 2011 (edited) I am working on a script to search a text file but I cannot seem to come up with the code to do what I need. When an event happens, it has a time stamp. So I get the date and time stamp and search for that string in the file. Once I have it, I need to go down lines and pull the entire line. I will then pull the numbers at the end of this line. The problem I have is I do not know how to go down the seven (7) lines then get just the line with the numbers I need. Here is an example of the raw data I am searching for ... =========================================================================================================== Return-Path: noreply@timewarnercable.com X-OriginalArrivalTime: 05 Mar 2011 20:10:34.0099 (UTC) FILETIME=[6286F430:01CBDB71] --uVoice_BND4D7298B9127949 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mailbox 919-529-4460 has a new voice message from Unknown Name (919-528-9245). =========================================================================================================== The second line of data is where I will o the initial search, so I know how to code that. What I need some guidance on is how to go and get the line (last line in the example above) that has the phone number at the end of the line. Can someone point me in the right direction? Barry Edited March 7, 2011 by bqbowden
PsaltyDS Posted March 6, 2011 Posted March 6, 2011 It's easiest to do from an array: #include <File.au3> #include <Array.au3> Global $sDateTime = "05 Mar 2011 20:10:34.0099" Global $sLogFile = @ScriptDir & "\Test1.txt" Global $aLogData[1], $iIndex ; Find Date\Time stamp _FileReadToArray($sLogFile, $aLogData) $iIndex = _ArraySearch($aLogData, $sDateTime, 1, 0, 0, 1) If @error Then MsgBox(16, "Error", "Failed to find Date\Time stamp: " & $sDateTime) Exit EndIf ; Display seven lines after Date\Time stamp $iIndex += 7 If $iIndex <= $aLogData[0] Then MsgBox(64, "Data line", $aLogData[$iIndex]) Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
bqbowden Posted March 6, 2011 Author Posted March 6, 2011 Great, thanks. I am not sure I understand everything I am looking at, but I will pick it apart. This will help. Barry
bqbowden Posted March 7, 2011 Author Posted March 7, 2011 It's easiest to do from an array: #include <File.au3> #include <Array.au3> Global $sDateTime = "05 Mar 2011 20:10:34.0099" Global $sLogFile = @ScriptDir & "\Test1.txt" Global $aLogData[1], $iIndex ; Find Date\Time stamp _FileReadToArray($sLogFile, $aLogData) $iIndex = _ArraySearch($aLogData, $sDateTime, 1, 0, 0, 1) If @error Then MsgBox(16, "Error", "Failed to find Date\Time stamp: " & $sDateTime) Exit EndIf ; Display seven lines after Date\Time stamp $iIndex += 7 If $iIndex <= $aLogData[0] Then MsgBox(64, "Data line", $aLogData[$iIndex]) Well, after several hours of tweaking, the above script PsaltyDS was kind enough to give me is not working and I have no idea why. Here is the code and some sample data. #include <File.au3> #include <Array.au3> Global $sDateTime = "X-OriginalArrivalTime: 07 Mar 2011 21:08" Global $sLogFile = @ScriptDir & "C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox" Global $aLogData[1], $iIndex ; Find Date\Time stamp _FileReadToArray($sLogFile, $aLogData) $iIndex = _ArraySearch($aLogData, $sDateTime, 1, 0, 0, 1) If @error Then MsgBox(16, "Error", "Failed to find Date\Time stamp: " & $sDateTime) Exit EndIf ; Display seven lines after Date\Time stamp $iIndex += 7 If $iIndex <= $aLogData[0] Then MsgBox(64, "Data line", $aLogData[$iIndex]) DATA ============================================================== To: FIRST NAME LAST NAME <bqbowden@msn.com> Return-Path: noreply@timewarnercable.com X-OriginalArrivalTime: 07 Mar 2011 21:08:07.0775 (UTC) FILETIME=[C1E796F0:01CBDD0B] --uVoice_BND4D754936615238 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mailbox 919-529-4460 has a new voice message from Unknown Name (919-575-4175). Length: 17 seconds Received on: 03/07/2011 04:08PM (EST) DATA ============================================================== Why is the search failing to find the text? Barry
omikron48 Posted March 8, 2011 Posted March 8, 2011 (edited) Global $sLogFile = @ScriptDir & "C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox" This is causing your error. The actual file path contained in $sLogFile is invalid since it's a concatenation of the file path of your script and the "C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox" Since the file path you are trying to read is invalid, you can't read anything because the file you want can't be found. Try printing out the value of $sLogFile to see what I mean. You can just stick this line after you assign a value to $sLogFile. MsgBox(0x2000, "sLogFile", $sLogFile) Edited March 8, 2011 by omikron48
bqbowden Posted March 8, 2011 Author Posted March 8, 2011 Global $sLogFile = @ScriptDir & "C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox" This is causing your error. The actual file path contained in $sLogFile is invalid since it's a concatenation of the file path of your script and the "C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox" Since the file path you are trying to read is invalid, you can't read anything because the file you want can't be found. Try printing out the value of $sLogFile to see what I mean. You can just stick this line after you assign a value to $sLogFile. MsgBox(0x2000, "sLogFile", $sLogFile) Omikron48, I will give this a try. I have validated the file path name and know that is correct because I use the same path in another script I wrote. But it may be getting truncated for some reason in this script. I will let you know what happens. Barry
kylomas Posted March 8, 2011 Posted March 8, 2011 (edited) bqbowden, As omikron48 suggests run something like the following: Global $sLogFile = @ScriptDir & "C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox" consolewrite($slogfile) I don't see how any previous script could be working if the filename is derived like this. kylomas Edited March 8, 2011 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
bqbowden Posted March 8, 2011 Author Posted March 8, 2011 bqbowden, As omikron48 suggests run something like the following: Global $sLogFile = @ScriptDir & "C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox" consolewrite($slogfile) I don't see how any previous script could be working if the filename is derived like this. kylomas Here is the script I have been running to test another phase of this project. It uses the same filename and works with no problem. It is using a different command but the file is the same. ; === VARIABLE DECLARATIONS === ;$file = Mozilla Thunderbird Inbox for all messages from my MSN account $file = FileRead("C:\Documents and Settings\Barry Bowden\Application Data\Thunderbird\Profiles\8xdu7029.default\Mail\pop3.live.com\Inbox") & @LF $total = Amount($file,"new voice message") $Mother = Amount($file, "919-575-4175") $Dad = Amount($file, "919-906-3060") $Other = $total - ($Mother + $Dad) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf If $Total < 0 Then $Total = 0 MsgBox(0, "VM Count", "Total VMs = "& $Total & @CRLF & "Mother = " & $Mother & @CRLF & "Dad = "& $Dad & @CRLF & "Other = " & $Other) Exit ;=== FUNCTIONS === Func Amount($string, $search) Local $aCount = StringRegExp($string, ".*(?i:" & $search & ").*\n", 3) Return (UBound($aCount)) EndFunc
kylomas Posted March 8, 2011 Posted March 8, 2011 bqbowden, Look at what you are doing, CLOSELY, and you will see what is going wrong. If you run the code that I posted it will point you in the right direction. kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
bqbowden Posted March 8, 2011 Author Posted March 8, 2011 bqbowden, Look at what you are doing, CLOSELY, and you will see what is going wrong. If you run the code that I posted it will point you in the right direction.kylomasI am so stupid - "@ScriptDir" was adding extra stuff to the path so OF COURSE it would never find it. So, the "@ScriptDir" command puts in where the script is running from.Thanks a lot for pointing me to this.Barry
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