Jump to content

Stuck on the IF condition


Recommended Posts

Got a txt file that looks like this:

Date: 05/21/2011,Time: 00:00-00:30,5180401,6,0:00,0:00,0:00,0,0,0,0:00,0,0,0,0,0,0,0,0,0,0,0,0

Date: 05/21/2011,Time: 00:00-00:30,5180403,1,0:00,0:00,0:00,0,0,0,0:00,0,0,0,0,0,0,0,0,0,0,0,0

Date: 05/22/2011,Time: 00:30-01:00,5180401,6,0:00,0:00,0:00,0,0,0,0:00,0,0,0,0,0,0,0,0,0,0,0,0

Date: 05/22/2011,Time: 01:30-02:00,5181401,1,0:00,0:00,0:00,0,0,0,0:00,0,0,0,0,0,0,0,0,0,0,0,0

Date: 05/23/2011,Time: 02:00-02:30,5180501,2,0:00,0:00,0:00,0,0,0,0:00,0,0,0,0,0,0,0,0,0,0,0,0

I have to break the file down into multiple files by date+time. For each date and time "group", I need to out the data starting from the 518xxxx to the end of the line. So the above should produce 4 files.

Here is what I have so far. The files are getting created, but they are empty. Not sure where my problem is.

Can anyone assist please? Fast approaching my deadline with nothing of use!

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_Outfile=C:\avaya\VecAnnFile.exe

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <Date.au3>

#include <File.au3>

$file = FileOpen("c:\test\out521.csv", 0)

$result = ""

$vnum = ""

$ann = ""

$logfile = ""

$vtime = ""

$vdate = ""

$pt = ""

$pd = ""

$rr = ""

$ss = ""

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

;grab the date and time of the line

$vdate = StringMid($line, 1, 17)

$vtime = StringMid($line, 19, 18)

;create a file with random name

$ann = Random(10, 10000, 1)

$logfile = "c:\test\" & $ann & ".txt"

FileOpen($logfile, 1)

;compare dates

$rr=StringCompare($vdate,$pd,2)

If $rr = 0 Then

;compare times

$ss = StringCompare($vtime,$pt,2)

If $ss = 0 Then

$result = StringMid($line, 38, 70)

FileWrite($logfile, $result & @CRLF)

$pd = $vdate

$pt = $vtime

EndIf

EndIf

FileClose($logfile)

WEnd

Link to comment
Share on other sites

  • Moderators

deef99,

I rewrote the whole thing and I think it does what you want now: ;)

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

Global $result, $hLogFile, $vtime, $vdate, $pt, $pd
Global $aLines

_FileReadToArray("data.txt", $aLines)

For $i = 1 To $aLines[0]

    $line = $aLines[$i]
    ;grab the date and time of the line
    $vdate = StringMid($line, 7, 10)
    $vtime = StringMid($line, 24, 11)

    ; If either date or time change
    If $vdate <> $pd Or $vtime <> $pt Then

        ConsoleWrite("Opening new file at line " & $i & @CRLF)

        ; Save current date/time
        $pd = $vdate
        $pt = $vtime

        ; Close Existing file
        FileClose($hLogFile)
        ; Create a new file with random name
        $ann = Random(10, 10000, 1)
        $hLogFile = @ScriptDir & "\" & $ann & ".txt"
        FileOpen($hLogFile, 1)

    EndIf

    $result = StringMid($line, 36, 72)
    FileWrite($hLogFile, $result & @CRLF)

Next

FileClose($hLogFile)

Please ask if you have any questions. :huh2:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Ask a question??? OMG!!!! I am overwhelmed with your ability to write new code so quickly...thank you so much!

I ran it...and it is breaking the file at the right places...but I am not getting any files created.

I did change @scriptdir to C:\test...did I do that incorrectly? Again, really, can't thank you enough for your generosity!

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_Outfile=C:\avaya\VecAnnFile.exe

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <Date.au3>

#include <File.au3>

Global $result, $hLogFile, $vtime, $vdate, $pt, $pd

Global $aLines

_FileReadToArray("c:\test\out521.csv", $aLines)

For $i = 1 To $aLines[0]

$line = $aLines[$i]

;grab the date and time of the line

$vdate = StringMid($line, 7, 10)

$vtime = StringMid($line, 24, 11)

; If either date or time change

If $vdate <> $pd Or $vtime <> $pt Then

ConsoleWrite("Opening new file at line " & $i & @CRLF)

; Save current date/time

$pd = $vdate

$pt = $vtime

; Close Existing file

FileClose($hLogFile)

; Create a new file with random name

$ann = Random(10, 10000, 1)

$hLogFile = "c:\test" & $ann & ".txt"

FileOpen($hLogFile, 1)

EndIf

$result = StringMid($line, 36, 72)

FileWrite($hLogFile, $result & @CRLF)

Next

FileClose($hLogFile)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...