Sign in to follow this  
Followers 0
lhk69

_StringBetween?

15 posts in this topic

Hi.

Ok, I know I am probably NOT going about this solution the best way. Im new and not the world’s greatest programmer.

But, what I need to do is read through this report (see code snippet below for sample report data) and grab each "Server Name", "Current Group" and "The DATE that each step was completed.

I got everything BUT the dates. I can find the line, but not grab JUST the date and time from that line. Eventually in delimited format. (Server, Group, Date, Date, Date.... ) What am I doing wrong? I know _StringBetween returns an array. Im just not sure how to get the results out of it that I need.

I probably need another function? Maybe I shouldn’t read file in as array - read line by line? I’m just not sure the best way to do this. HELP!!

CURRENT ATTEMPT:

#include <File.au3>
#include <array.au3>
#include <String.au3>

Dim $myArray
Dim $textStageDate[1]


$InputFile = "c:\documents and settings\v-kleinl\desktop\report.rep"

If $InputFile = -1 Then
    MsgBox(0, "Error", "Unable to open Input file.")
    Exit
EndIf

$OutputFile = FileOpen("c:\documents and settings\v-kleinl\desktop\results.csv", 10)

If $OutputFile = -1 Then
    MsgBox(0, "Error", "Unable to open Output file.")
    Exit
EndIf


_FileReadToArray($InputFile, $myArray)

For $x = 1 To $myArray[0]

    If StringLeft($myArray[$x], 14) = "Server Name : " Then
        ;msgbox(0,"FOUND server name on line " & $x, $myArray[$x])
        $textServer = StringMid($myArray[$x], 15, 15)
        FileWrite($OutputFile, @CRLF)
        FileWriteLine($OutputFile, $textServer)
        ;FileWrite($OutputFile,$textServer & ",")
    EndIf

    If StringLeft($myArray[$x], 16) = "Current Group : " Then
        ;msgbox(0,"FOUND Current Group on line " & $x, $myArray[$x])
        $textGroup = StringMid($myArray[$x], 17, 35)
        FileWriteLine($OutputFile, $textGroup)
        ;FileWrite($OutputFile,$textGroup & ",")
    EndIf

    If StringLeft($myArray[$x], 30) = "   Current Process Step : Step" Then
        ;msgbox(0,"FOUND a step on line " & $x,$myArray[$x])
        ;$textStageStart = StringInStr($myArray[$x], "completed")
        ;$textStageDate = StringMid($myArray[$x],$textStageStart,34)
        $textStageDate = _StringBetween($myArray[$x], "completed on ", "by ")
        FileWriteLine($OutputFile, $textStageDate[0])
        ;FileWrite($OutputFile,$textStageDate & ",")
    EndIf

Next

Sleep(500)

FileClose($InputFile)
FileClose($OutputFile)

REPORT EXAMPLE:

Server Name : SERVER_01
Current Group : Completed
Activity Log : 10/26/2010 7:29:14 AM v-bhowma
   Current Process Step : Planning completed on 10/26/2010 7:29:15 AM by v-bhowma
   
   10/26/2010 7:29:18 AM v-bhowma
   Current Process Step : Step1 - Order Server completed on 10/26/2010 7:29:19 AM by v-bhowma
   
   10/26/2010 12:05:29 PM v-arunas
   Current Process Step : Step2 - Provide Network Facilities & Data completed on 10/26/2010 12:05:28 PM by v-arunas
   
   10/28/2010 5:07:58 PM reesebj
   Current Process Step : Step3 - Facilities Prep completed on 10/28/2010 5:07:58 PM by reesebj
   
   10/28/2010 5:08:01 PM reesebj
   Current Process Step : Step4 - Server Receipt completed on 10/28/2010 5:08:01 PM by reesebj
   
   11/1/2010 11:56:30 AM v-bhowma
   Current Process Step : Step5 - Confirm Configuration completed on 11/1/2010 11:56:30 AM by v-bhowma
   
   11/1/2010 11:56:35 AM v-bhowma
   Current Process Step : Step6 - Physical Install in Initial Location completed on 11/1/2010 11:56:34 AM by v-bhowma
   
   11/1/2010 11:56:39 AM v-bhowma
   Current Process Step : Step7 - Stage Server and Configure SA completed on 11/1/2010 11:56:38 AM by v-bhowma
   
   11/5/2010 9:15:21 PM reesebj
   Current Process Step : Step8 - Move Server to Production Floor or Ship to Location completed on 11/5/2010 9:15:21 PM by reesebj
   
   11/15/2010 2:42:33 PM reesebj
   Current Process Step : Step 9 - Physical Install at Final Location completed on 11/15/2010 2:42:33 PM by reesebj
   
   11/17/2010 7:40:37 AM v-bhowma
   Current Process Step : Step 10 - Configure SAN Disk completed on 11/17/2010 7:40:36 AM by v-bhowma
   
   11/17/2010 7:40:40 AM v-bhowma
   Current Process Step : Step 11 - Configure Server for Intended Use completed on 11/17/2010 7:40:39 AM by v-bhowma
   
   1/21/2011 5:41:20 PM v-chandd
   Current Process Step : Step 12 - Backups completed on 1/21/2011 5:41:19 PM by v-chandd
   
   1/25/2011 3:13:40 PM v-isaacb
   Current Process Step : Step 13 - Monitoring completed on 1/25/2011 3:13:40 PM by v-isaacb
   
   1/26/2011 7:08:58 AM v-bhowma
   Current Process Step : Step 14 - Security Scans & Remediation completed on 1/26/2011 7:08:57 AM by v-bhowma
   
   1/27/2011 8:40:24 AM reesebj
   Final Turnover Approval for their group was given on 01/27/11 08:40:24 by reesebj
   
   1/27/2011 5:41:33 PM morgarl
   Final Turnover Approval for their group was given on 01/27/11 17:41:33 by morgarl
   
   2/2/2011 1:58:35 PM kenneac1
   Final Turnover Approval for their group was given on 02/02/11 13:58:35 by kenneac1
   
   2/4/2011 3:15:22 PM bokonb
   Final Turnover Approval for their group was given on 02/04/11 15:15:22 by bokonb
   
   2/5/2011 6:02:55 PM v-giriu


   Final Turnover Approval for their group was given on 02/05/11 18:02:55 by v-giriu
   
   2/8/2011 8:32:06 PM v-arunas
   Final Turnover Approval for their group was given on 02/08/11 20:32:06 by v-arunas
   
   2/9/2011 6:21:45 AM v-bhowma
   Final Turnover Approval for their group was given on 02/09/11 06:21:45 by v-bhowma
   
   2/15/2011 8:01:19 AM v-pandas1
   Final Turnover Approval for their group was given on 02/15/11 08:01:19 by v-pandas1
   
   2/15/2011 9:52:35 AM reesebj
   Current Process Step : Step 15 - QA completed on 2/15/2011 9:52:35 AM by reesebj
   
                                                                                                                                                      
Server Name : SERVER_02
Current Group : Completed
Activity Log : 10/26/2010 7:32:26 AM v-bhowma
   Current Process Step : Planning completed on 10/26/2010 7:32:27 AM by v-bhowma
   
   10/26/2010 7:32:30 AM v-bhowma
   Current Process Step : Step1 - Order Server completed on 10/26/2010 7:32:31 AM by v-bhowma
   
   10/26/2010 12:05:35 PM v-arunas
   Current Process Step : Step2 - Provide Network Facilities & Data completed on 10/26/2010 12:05:35 PM by v-arunas
   
   10/28/2010 5:08:05 PM reesebj
   Current Process Step : Step3 - Facilities Prep completed on 10/28/2010 5:08:05 PM by reesebj
   
   10/28/2010 5:08:08 PM reesebj
   Current Process Step : Step4 - Server Receipt completed on 10/28/2010 5:08:08 PM by reesebj
   
   11/1/2010 11:56:47 AM v-bhowma
   Current Process Step : Step5 - Confirm Configuration completed on 11/1/2010 11:56:47 AM by v-bhowma
   
   11/1/2010 11:56:50 AM v-bhowma
   Current Process Step : Step6 - Physical Install in Initial Location completed on 11/1/2010 11:56:50 AM by v-bhowma
   
   11/1/2010 11:56:54 AM v-bhowma
   Current Process Step : Step7 - Stage Server and Configure SA completed on 11/1/2010 11:56:53 AM by v-bhowma
   
   11/5/2010 9:15:31 PM reesebj
   Current Process Step : Step8 - Move Server to Production Floor or Ship to Location completed on 11/5/2010 9:15:31 PM by reesebj
   
   11/15/2010 2:42:37 PM reesebj
   Current Process Step : Step 9 - Physical Install at Final Location completed on 11/15/2010 2:42:37 PM by reesebj
   
   11/17/2010 7:40:48 AM v-bhowma
   Current Process Step : Step 10 - Configure SAN Disk completed on 11/17/2010 7:40:47 AM by v-bhowma
   
   11/17/2010 7:40:51 AM v-bhowma
   Current Process Step : Step 11 - Configure Server for Intended Use completed on 11/17/2010 7:40:50 AM by v-bhowma
   
   1/21/2011 5:41:11 PM v-chandd
   Current Process Step : Step 12 - Backups completed on 1/21/2011 5:41:10 PM by v-chandd
   
   1/25/2011 3:13:27 PM v-isaacb
   Current Process Step : Step 13 - Monitoring completed on 1/25/2011 3:13:27 PM by v-isaacb
   
   1/26/2011 7:09:17 AM v-bhowma


   Current Process Step : Step 14 - Security Scans & Remediation completed on 1/26/2011 7:09:16 AM by v-bhowma
   
   1/27/2011 8:40:30 AM reesebj
   Final Turnover Approval for their group was given on 01/27/11 08:40:30 by reesebj
   
   1/27/2011 5:41:20 PM morgarl
   Final Turnover Approval for their group was given on 01/27/11 17:41:20 by morgarl
   
   2/2/2011 1:58:24 PM kenneac1
   Final Turnover Approval for their group was given on 02/02/11 13:58:24 by kenneac1
   
   2/4/2011 3:15:13 PM bokonb
   Final Turnover Approval for their group was given on 02/04/11 15:15:13 by bokonb
   
   2/5/2011 6:03:12 PM v-giriu
   Final Turnover Approval for their group was given on 02/05/11 18:03:12 by v-giriu
   
   2/8/2011 8:32:27 PM v-arunas
   Final Turnover Approval for their group was given on 02/08/11 20:32:26 by v-arunas
   
   2/9/2011 6:21:51 AM v-bhowma
   Final Turnover Approval for their group was given on 02/09/11 06:21:51 by v-bhowma
   
   2/15/2011 8:01:32 AM v-pandas1
   Final Turnover Approval for their group was given on 02/15/11 08:01:32 by v-pandas1
   
   2/15/2011 9:52:40 AM reesebj
   Current Process Step : Step 15 - QA completed on 2/15/2011 9:52:39 AM by reesebj
   
                                                                                                                                                      
Server Name : SERVER_03
Current Group : Completed
Activity Log : 10/26/2010 7:36:11 AM v-bhowma
   Current Process Step : Planning completed on 10/26/2010 7:36:12 AM by v-bhowma
   
   10/26/2010 7:36:16 AM v-bhowma
   Current Process Step : Step1 - Order Server completed on 10/26/2010 7:36:16 AM by v-bhowma
   
   10/26/2010 12:05:39 PM v-arunas
   Current Process Step : Step2 - Provide Network Facilities & Data completed on 10/26/2010 12:05:38 PM by v-arunas
   
   10/28/2010 5:08:12 PM reesebj
   Current Process Step : Step3 - Facilities Prep completed on 10/28/2010 5:08:11 PM by reesebj
   
   10/28/2010 5:08:14 PM reesebj
   Current Process Step : Step4 - Server Receipt completed on 10/28/2010 5:08:14 PM by reesebj
   
   11/1/2010 11:57:02 AM v-bhowma
   Current Process Step : Step5 - Confirm Configuration completed on 11/1/2010 11:57:01 AM by v-bhowma
   
   11/1/2010 11:57:05 AM v-bhowma
   Current Process Step : Step6 - Physical Install in Initial Location completed on 11/1/2010 11:57:05 AM by v-bhowma
   
   11/1/2010 11:57:09 AM v-bhowma
   Current Process Step : Step7 - Stage Server and Configure SA completed on 11/1/2010 11:57:09 AM by v-bhowma
   
   11/5/2010 9:15:36 PM reesebj
   Current Process Step : Step8 - Move Server to Production Floor or Ship to Location completed on 11/5/2010 9:15:36 PM by reesebj
   
   11/15/2010 2:42:42 PM reesebj


   Current Process Step : Step 9 - Physical Install at Final Location completed on 11/15/2010 2:42:41 PM by reesebj
   
   11/17/2010 7:40:56 AM v-bhowma
   Current Process Step : Step 10 - Configure SAN Disk completed on 11/17/2010 7:40:55 AM by v-bhowma
   
   11/17/2010 7:41:00 AM v-bhowma
   Current Process Step : Step 11 - Configure Server for Intended Use completed on 11/17/2010 7:40:58 AM by v-bhowma
   
   1/21/2011 5:41:01 PM v-chandd
   Current Process Step : Step 12 - Backups completed on 1/21/2011 5:41:00 PM by v-chandd
   
   1/25/2011 3:13:14 PM v-isaacb
   Current Process Step : Step 13 - Monitoring completed on 1/25/2011 3:13:14 PM by v-isaacb
   
   1/26/2011 7:09:32 AM v-bhowma
   Current Process Step : Step 14 - Security Scans & Remediation completed on 1/26/2011 7:09:31 AM by v-bhowma
   
   1/27/2011 8:40:36 AM reesebj
   Final Turnover Approval for their group was given on 01/27/11 08:40:36 by reesebj
   
   1/27/2011 5:41:07 PM morgarl
   Final Turnover Approval for their group was given on 01/27/11 17:41:06 by morgarl
   
   2/2/2011 1:58:12 PM kenneac1
   Final Turnover Approval for their group was given on 02/02/11 13:58:12 by kenneac1
   
   2/4/2011 3:14:57 PM bokonb
   Final Turnover Approval for their group was given on 02/04/11 15:14:57 by bokonb
   
   2/5/2011 6:03:25 PM v-giriu
   Final Turnover Approval for their group was given on 02/05/11 18:03:25 by v-giriu
   
   2/8/2011 8:32:42 PM v-arunas
   Final Turnover Approval for their group was given on 02/08/11 20:32:42 by v-arunas
   
   2/9/2011 8:52:29 AM v-bhowma
   Final Turnover Approval for their group was given on 02/09/11 08:52:29 by v-bhowma
   
   2/15/2011 8:01:43 AM v-pandas1
   Final Turnover Approval for their group was given on 02/15/11 08:01:43 by v-pandas1
   
   2/15/2011 9:52:44 AM reesebj
   Current Process Step : Step 15 - QA completed on 2/15/2011 9:52:44 AM by reesebj

Thank you in advance for any help/pointers.

Share this post


Link to post
Share on other sites



hi lhk69,

Perhaps you can look at StringRegExp in the help-file.

Also can you post a few sample lines of how you will like your results.csv to look based on the report example you posted?

Regards,

-smartee

Share this post


Link to post
Share on other sites

Hi smartee.

Thank you for the reply. Yes, I did look at Reg Expressions, but I can't figure that out either - YET. I will continue to look at it.

Anyway, here is what I'd like the result to look like.

SERVER_01,Completed,10/26/2010 7:29:19 AM,10/26/2010 12:05:28 P,10/28/2010 5:07:58 PM,10/28/2010 5:08:01 PM,11/1/2010 11:56:30 AM,11/1/2010 11:56:34 AM,11/1/2010 11:56:38 AM,11/5/2010 9:15:21 PM ,11/15/2010 2:42:33 PM,11/17/2010 7:40:36 AM,11/17/2010 7:40:39 AM,1/21/2011 5:41:19 PM ,1/25/2011 3:13:40 PM ,1/26/2011 7:08:57 AM ,2/15/2011 9:52:35 AM ,
SERVER_02,Completed,10/26/2010 7:32:31 AM,10/26/2010 12:05:35 P,10/28/2010 5:08:05 PM,10/28/2010 5:08:08 PM,11/1/2010 11:56:47 AM,11/1/2010 11:56:50 AM,11/1/2010 11:56:53 AM,11/5/2010 9:15:31 PM ,11/15/2010 2:42:37 PM,11/17/2010 7:40:47 AM,11/17/2010 7:40:50 AM,1/21/2011 5:41:10 PM ,1/25/2011 3:13:27 PM ,1/26/2011 7:09:16 AM ,2/15/2011 9:52:39 AM ,
SERVER_03,Completed,10/26/2010 7:36:16 AM,10/26/2010 12:05:38 P,10/28/2010 5:08:11 PM,10/28/2010 5:08:14 PM,11/1/2010 11:57:01 AM,11/1/2010 11:57:05 AM,11/1/2010 11:57:09 AM,11/5/2010 9:15:36 PM ,11/15/2010 2:42:41 PM,11/17/2010 7:40:55 AM,11/17/2010 7:40:58 AM,1/21/2011 5:41:00 PM ,1/25/2011 3:13:14 PM ,1/26/2011 7:09:31 AM ,2/15/2011 9:52:44 AM ,
SERVER_01,Completed,10/26/2010 7:40:01 AM,10/26/2010 12:05:47 P,10/28/2010 5:08:18 PM,10/28/2010 5:08:20 PM,11/1/2010 11:57:15 AM,11/1/2010 11:57:18 AM,11/1/2010 11:57:22 AM,11/5/2010 9:15:41 PM ,11/15/2010 2:42:46 PM,11/17/2010 7:41:05 AM,11/17/2010 7:41:08 AM,1/21/2011 5:40:48 PM ,1/25/2011 3:13:00 PM ,1/26/2011 7:09:48 AM ,2/15/2011 9:52:48 AM ,

Share this post


Link to post
Share on other sites

FYI -

_StringBetween returns an array, not a literal string.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

@SmOke_N: Yes it does, but he seems to already know that not so? :) He uses it above like this

$textStageDate = _StringBetween($myArray[$x], "completed on ", "by ")
FileWriteLine($OutputFile, $textStageDate[0])

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I think you've gone about the task in a perfectly fine manner, and your code works just fine for me:

#include <File.au3>
#include <array.au3>
#include <String.au3>

Global $myArray, $OutputLine, $textStageDate[1]
Global $InputFile = "report.rep"

If Not _FileReadToArray($InputFile, $myArray) Then
    MsgBox(0, "Error", "Unable to open Input file.")
    Exit
EndIf

$OutputFile = FileOpen("results.csv", 10)
If $OutputFile = -1 Then
    MsgBox(0, "Error", "Unable to open Output file.")
    Exit
EndIf

For $x = 1 To $myArray[0]
    If StringLeft($myArray[$x], 14) = "Server Name : " Then
        FileWrite($OutputFile, $OutputLine & @CRLF)
        $OutputLine = StringMid($myArray[$x], 15, 15) & ","
    EndIf
    If StringLeft($myArray[$x], 16) = "Current Group : " Then
        $OutputLine &= StringMid($myArray[$x], 17, 35) & ","
    EndIf
    If StringLeft($myArray[$x], 30) = "   Current Process Step : Step" Then
        $textStageDate =  _StringBetween($myArray[$x], "completed on ", " by ")
        $OutputLine &= $textStageDate[0] & ","
    EndIf
Next
FileWrite($OutputFile, $OutputLine & @CRLF)
FileClose($InputFile)
FileClose($OutputFile)

Edit: oops, put your filenames back to what they were...

Edited by Spiff59

Share this post


Link to post
Share on other sites

Good Morning, Spiff.

Thats what I thought too. BUT, i get this error:

(29) : ==> Subscript used with non-Array variable.:
$OutputLine &= $textStageDate[0] & ","
$OutputLine &= $textStageDate^ ERROR

the inputfile is about 800 lines. It seems to read about 80 or 90 lines, and then it errors out with the above error.

If you would, please copy and paste the lines in the input file a dozen or so times and try again.

Thank you for your trouble.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Good Morning, Spiff.

Thats what I thought too. BUT, i get this error:

(29) : ==> Subscript used with non-Array variable.:
$OutputLine &= $textStageDate[0] & ","
$OutputLine &= $textStageDate^ ERROR

the inputfile is about 800 lines. It seems to read about 80 or 90 lines, and then it errors out with the above error.

If you would, please copy and paste the lines in the input file a dozen or so times and try again.

Thank you for your trouble.

It still works fine for me with the input duplicated 12 times.

You apparently have an exception in your input data that is not resulting in an array returned from the StringBetween() statement. To cause your error, it would have to be a line beginning with " Current Process Step : Step" but not containing both "completed on " and " by ". You could modify your code to handle such exceptions, or simply ignore them with an "If Not _IsArray($textStageDate) Then ContinueLoop" after the StringBetween() statement.

typo

Edited by Spiff59

Share this post


Link to post
Share on other sites

Check to see if $textStageDate is an Array before executing that line using something like this

$textStageDate =  _StringBetween($myArray[$x], "completed on ", " by ")
        If IsArray($textStageDate) then
            $OutputLine &= $textStageDate[0] & ","
        Endif

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I tried the Regular Expression idea as well. This is as close as I could come. I also did a FileReadLine loop, instead of an FileReadToArray.

But, now, it picks up BOTH date lines and given me duplicate date entrien in the output. I thought I was only acting on the line that contained " Current Process Step : Step"

SIGH.....

#include <File.au3>
#include <array.au3>
#include <String.au3>

Dim $myArray
Dim $textStageDate[1]


$InputFile = FileOpen("report.rep",0)

If $InputFile = -1 Then
    MsgBox(0, "Error", "Unable to open Input file.")
    Exit
EndIf

$OutputFile = FileOpen("results.csv", 10)

If $OutputFile = -1 Then
    MsgBox(0, "Error", "Unable to open Output file.")
    Exit
EndIf


While 1
    $line_read = FileReadLine($InputFile)
    If @error = -1 Then ExitLoop
    ;msgbox(0,"Line Read", $line_read )

    If StringLeft($line_read , 14) = "Server Name : " Then
        ;msgbox(0,"FOUND server name", $line_read )
        $textServer = StringMid($line_read , 15, 15)
        FileWrite($OutputFile, @CRLF)
        ;FileWriteLine($OutputFile, $textServer)
        FileWrite($OutputFile,$textServer & ",")
    EndIf

    If StringLeft($line_read, 16) = "Current Group : " Then
        ;msgbox(0,"FOUND Current Group on line " & $x, $myArray[$x])
        $textGroup = StringMid($line_read, 17, 35)
        ;FileWriteLine($OutputFile, $textGroup)
        FileWrite($OutputFile,$textGroup & ",")
    EndIf

    If StringLeft($line_read, 30) = "   Current Process Step : Step" Then
        ;msgbox(0,"FOUND a step on line " & $x,$myArray[$x])
        $textStageDate = StringRegExp($line_read,'(\d+[/]\d+[/]\d+\s\d+\S\d+\S\d+\S\s\V\V)',2)
        for $i = 0 to UBound($textStageDate) - 1
            ;msgbox(0, "FOUND a date" , $textStageDate[$i])
            FileWrite($OutputFile,$textStageDate[$i] & ",")
            ;FileWriteLine($OutputFile, $textStageDate[0])
        Next
    EndIf


WEnd



FileClose($InputFile)
FileClose($OutputFile)

Share this post


Link to post
Share on other sites

Spiff.

You were absolutley right!!!

the "If Not IsArray($textStageDate) Then ContinueLoop" did the trick.

Again, thank you very much for your contribution.

You (and many others like you) are what makes AutoIt the best!!!

Share this post


Link to post
Share on other sites

How would you handle THIS case. Nearly all of the dates between "completed on" and "by" are in the XX/XX/XXXX format.

I just realized some are line this.....

completed on Wednesday, April 06, 2011 9:37:11 AM by

How would you handle this exception?

Meanwhile, I'l be trying to figure it out myself.

Thanks again.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

There are usually a million ways to do things, particularily string manipulation.

This was the first to come to mind:

#include <File.au3>
#include <array.au3>
#include <String.au3>

Global $myArray, $OutputLine, $textStageDate, $DateConvert
Global $MonthTable[13] = ["","January","February","March","April","May","June","July","August","September","October","November","December"]
Global $InputFile = "report.rep"

If Not _FileReadToArray($InputFile, $myArray) Then
    MsgBox(0, "Error", "Unable to open Input file.")
    Exit
EndIf

$OutputFile = FileOpen("results.csv", 10)
If $OutputFile = -1 Then
    MsgBox(0, "Error", "Unable to open Output file.")
    Exit
EndIf

For $x = 1 To $myArray[0]
    If StringLeft($myArray[$x], 14) = "Server Name : " Then
        FileWrite($OutputFile, $OutputLine & @CRLF)
        $OutputLine = StringMid($myArray[$x], 15, 15) & ","
    EndIf
    If StringLeft($myArray[$x], 16) = "Current Group : " Then
        $OutputLine &= StringMid($myArray[$x], 17, 35) & ","
    EndIf
    If StringLeft($myArray[$x], 30) = "   Current Process Step : Step" Then
        $textStageDate =  _StringBetween($myArray[$x], "completed on ", " by ")
        If Not IsArray($textStageDate) Then ContinueLoop
        $DateConvert = Stringsplit($textStageDate[0], ", ",1)
        If Not @error Then ; StringSplit() found comma/space delimiters so this is a "long date"
            $textStageDate[0] = $DateConvert[3] ; save the year
            $DateConvert = Stringsplit($DateConvert[2], " ") ; split month and day
            For $y = 1 to 12
                If $DateConvert[1] = $MonthTable[$y] Then
                    $DateConvert[1] = $y ; replace string month with numeric
                    ExitLoop
                EndIf
            Next
            $textStageDate[0] = $DateConvert[1] & "/" & Number($DateConvert[2]) & "/" & $textStageDate[0]
        EndIf
        $OutputLine &= $textStageDate[0] & ","
    EndIf
Next
FileWrite($OutputFile, $OutputLine & @CRLF)
FileClose($InputFile)
FileClose($OutputFile)

Edit: Just my observation to no one in particular... The StringSplit() function above, since it can return multiple elements, is a logical example of a routine returning an array as it's result. The reasoning for having StringBetween() return an array, when it can only generate a single element, eludes me completely.

Edited by Spiff59

Share this post


Link to post
Share on other sites

WOW. Your way is MUCH cleaner and nicer. I also discovered another malformed date. "2011.23.04" So, I wrote another function. Look at all the ridiculous trouble I went through!!!

Thanks again for your help.

#include <File.au3>
#include <array.au3>
#include <String.au3>
#include <Date.au3>

Global $myArray, $OutputLine, $textStageDate[1]
Global $InputFile = "report.rep"

Func Fix_Date_1($input)
    ;Replace comma with space
    Local $temp0 = StringReplace($input, ",", "")

    ;Remove the DAY
    If StringLeft($temp0, 3) = "Mon" Then
        Local $temp1 = StringReplace($temp0, "Monday ", "")
    EndIf
    If StringLeft($temp0, 3) = "Tue" Then
        Local $temp1 = StringReplace($temp0, "Tuesday ", "")
    EndIf
    If StringLeft($temp0, 3) = "Wed" Then
        Local $temp1 = StringReplace($temp0, "Wednesday ", "")
    EndIf
    If StringLeft($temp0, 3) = "Thu" Then
        Local $temp1 = StringReplace($temp0, "Thursday ", "")
    EndIf
    If StringLeft($temp0, 3) = "Fri" Then
        Local $temp1 = StringReplace($temp0, "Friday ", "")
    EndIf
    If StringLeft($temp0, 3) = "Sat" Then
        Local $temp1 = StringReplace($temp0, "Saturday ", "")
    EndIf
    If StringLeft($temp0, 3) = "Sun" Then
        Local $temp1 = StringReplace($temp0, "Sunday ", "")
    EndIf

    ;Split up result into array
    Local $temp = StringSplit($temp1, " ")

    ;Convert Month into a numeric value
    Local $month = StringSplit("Jan,Feb,Mar,Apr,May,June,July,Aug,Sept,Oct,Nov,Dec", ",")

    For $x = 1 To 12
        If StringInStr($temp[1], $month[$x]) Then Return $x & "/" & $temp[2] & "/" & $temp[3] & " " & $temp[4] & " " & $temp[5]
    Next

    Return SetError(1, 0, -1)

EndFunc


Func Fix_Date_2($input)
    ;Replace comma with space
    Local $temp0 = StringReplace($input, ".", " ")

    ;Split up result into array
    Local $temp = StringSplit($temp0, " ")
    ;_ArrayDisplay($temp)

Return  $temp[2] & "/" & $temp[3] & "/" & $temp[1] & " " & $temp[5]
Return SetError(1, 0, -1)

EndFunc


If Not _FileReadToArray($InputFile, $myArray) Then
    MsgBox(0, "Error", "Unable to open Input file.")
    Exit
EndIf

$OutputFile = FileOpen("results.csv", 10)
If $OutputFile = -1 Then
    MsgBox(0, "Error", "Unable to open Output file.")
    Exit
EndIf

For $x = 1 To $myArray[0]
    If StringLeft($myArray[$x], 14) = "Server Name : " Then
        FileWrite($OutputFile, $OutputLine & @CRLF)
        $OutputLine = StringMid($myArray[$x], 15, 15) & ","
    EndIf

    If StringLeft($myArray[$x], 16) = "Current Group : " Then
        $OutputLine &= StringMid($myArray[$x], 17, 35) & ","
    EndIf

    If StringLeft($myArray[$x], 30) = "   Current Process Step : Step" Then

        $textStageDate = _StringBetween($myArray[$x], "completed on ", " by ")

        If Not IsArray($textStageDate) Then ContinueLoop

        If StringLeft($textStageDate[0], 5) = "2011." Then
            $FixedDate = Fix_Date_2($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf


        If StringLeft($textStageDate[0], 3) = "Mon" Then
            $FixedDate = Fix_Date_1($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf

        If StringLeft($textStageDate[0], 3) = "Tue" Then
            $FixedDate = Fix_Date_1($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf

        If StringLeft($textStageDate[0], 3) = "Wed" Then
            $FixedDate = Fix_Date_1($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf

        If StringLeft($textStageDate[0], 3) = "Thu" Then
            $FixedDate = Fix_Date_1($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf
        If StringLeft($textStageDate[0], 3) = "Fri" Then
            $FixedDate = Fix_Date_1($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf

        If StringLeft($textStageDate[0], 3) = "Sat" Then
            $FixedDate = Fix_Date_1($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf

        If StringLeft($textStageDate[0], 3) = "Sun" Then
            $FixedDate = Fix_Date_1($textStageDate[0])
            $textStageDate[0] = $FixedDate
        EndIf

        $OutputLine &= $textStageDate[0] & ","

    EndIf
Next

FileWrite($OutputFile, $OutputLine & @CRLF)
FileClose($InputFile)
FileClose($OutputFile)

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Is this working for you?

#Include <Date.au3>
Global Const $sep = ";"
Global Const $file = FileRead("Report.txt")
Global Const $aT = StringSplit($file, @LF, 2)
If Not IsArray($aT) Then Exit
Global $csv, $i, $y, $z, $cd
$z = 0
$y = ""
For $i = 0 To UBound($aT) - 1
    $line = StringStripCR($aT[$i])
    If $z Then $y = @LF
    $c = StringRegExpReplace($line, "Server Name : ", "")
    If @extended Then $csv &= $y & $c & $sep
    $aC = StringRegExp($line, "(?i)(completed on) (\d+/\d+/\d+ \d+:\d+:\d+ [AM|PM]+) (.*)", 3)
    If IsArray($aC) Then
        $cd = _DateTimeFormat(StringRegExpReplace($aC[1], "(\d+)/(\d+)/(\d+)", "$3/$1/$2"), 1) & StringRegExpReplace($aC[1], "(\d+)/(\d+)/(\d+)", "")
        $csv &= $aC[0] &  " " & $cd &  " " &  $aC[2]  & $sep
    EndIf
    $z += 1
Next
Global Const $hFile = FileOpen("Output.csv", 2)
FileWrite($hFile, $csv)
FileClose($hFile)

If the report file is too large you can use this variant:

#Include <Date.au3>
Global Const $sep = ";"
Global $csv, $c, $aC, $y, $z, $cd
Global Const $hFileR = FileOpen("Report.txt")
Global Const $hFileW = FileOpen("Output.csv", 2)
$z = 0
$y = ""
While 1
    $line = FileReadLine($hFileR)
    If @error = -1 Then ExitLoop
    If $z Then $y = @LF
    $c = StringRegExpReplace($line, "Server Name : ", "")
    If @extended Then $csv &= $y & $c & $sep
    $aC = StringRegExp($line, "(?i)(completed on) (\d+/\d+/\d+ \d+:\d+:\d+ [AM|PM]+) (.*)", 3)
    If IsArray($aC) Then
        $cd = _DateTimeFormat(StringRegExpReplace($aC[1], "(\d+)/(\d+)/(\d+)", "$3/$1/$2"), 1) & StringRegExpReplace($aC[1], "(\d+)/(\d+)/(\d+)", "")
        $csv &= $aC[0] &  " " & $cd &  " " &  $aC[2]  & $sep
    EndIf
    $z += 1
Wend
FileClose($hFileR)
FileWrite($hFileW, $csv)
FileClose($hFileW)

Report.txt is the report example file you have provided.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0