Sign in to follow this  
Followers 0
erik7426

Help Replacing a CRLF

12 posts in this topic

A little background info...

I have a source file that will change from day to day. I am attaching a sample of this source file. The file is TAB delimited with 23 fields. The end result needs to be a pipe delimited file with place holders inserted between some of the fields. I wrote a script that did this nicely EXCEPT I have recently ran into an issue that I can't figure out how to fix.

Field23 contains a name. Most of the time it contains only one name and this field normally doesn't contain any quotes. Sometimes though it contains multiple names in quotes with the names separated by a CRLF character. When this occurs it breaks my script. My script is listed below. Any suggestions?

#include <Array.au3>
#include <String.au3>
#include <Date.au3>

$p = Chr(124)

$asterisk = Chr(42)

Dim $MyDate
Dim $MyTime

$TempDate = _DateAdd('D', -1, _NowCalc())
_DateTimeSplit($TempDate, $MyDate, $MyTime)
$FolderMonth = StringUpper(_DateToMonth($MyDate[2]))
$FolderYear = $MyDate[1]
$FileYear = StringRight($MyDate[1], 4)
If $MyDate[2] < 10 Then
    $FileMonth = "0" & $MyDate[2]
Else
    $FileMonth = $MyDate[2]
EndIf
If $MyDate[3] < 10 Then
    $FileDay = "0" & $MyDate[3]
Else
    $FileDay = $MyDate[3]
EndIf

Global $FileDate = $FileMonth & "-" & $FileDay & "-" & $FileYear


ConvertFile()

Func ConvertFile()
    $file = FileOpen(@ScriptDir & "\send.txt", 0)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    $DestFile = FileOpen(@ScriptDir & "\Send-" & $FileDate & ".txt", 10)
    If $DestFile = -1 Then
        MsgBox(0, "Error", "Unable to open destination file.")
        Exit
    EndIf

    $line = FileReadLine($file);This will skip the header row

    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $splitLine = StringSplit($line, @TAB)
        $my01 = $splitLine[1]
        $my02 = TimeFix($splitLine[2])
        $my03 = $splitLine[3]
        $my04 = $splitLine[4]
        $my05 = $splitLine[5]
        $my06 = $splitLine[6]
        $my07 = $splitLine[7]
        $my08 = $splitLine[8]
        $my09 = $splitLine[9]
        $my10 = $splitLine[10]
        $my11 = $splitLine[11]
        $my12 = $splitLine[12]
        $my13 = $splitLine[13]
        $my14 = TimeFix($splitLine[14])
        $my15 = TimeFix($splitLine[15])
        $my16 = TimeFix($splitLine[16])
        $my17 = TimeFix($splitLine[17])
        $my18 = TimeFix($splitLine[18])
        $my19 = $splitLine[19]
        $my20 = $splitLine[20]
        $my21 = $splitLine[21]
        $my22 = ""
        $my23 = ""
        $my24 = ""
        $my25 = StringUpper($splitLine[23])
        $my26 = StringUpper($splitLine[23])
        $my27 = ""
        $my28 = ""
        $my29 = ""
        $my30 = "";TimeFix($splitLine[23])
        $my31 = $splitLine[5]
        $my32 = ""
        $my33 = ""
        $my34 = ""
        $my35 = $asterisk

        $myLine = $my01 & $p & $my02 & $p & $my03 & $p & $my04 & $p & $my05 & $p & $my06 & $p & $my07 & $p & $my08 & $p & $my09 & $p & $my10 & $p & $my11 & $p & $my12 & $p & $my13 & $p & $my14 & $p & $my15 & $p & $my16 & $p & $my17 & $p & $my18 & $p & $my19 & $p & $my20 & $p & $my21 & $p & $my22 & $p & $my23 & $p & $my24 & $p & $my25 & $p & $my26 & $p & $my27 & $p & $my28 & $p & $my29 & $p & $my30 & $p & $my31 & $p & $my32 & $p & $my33 & $p & $my34 & $p & $my35

        FileWriteLine($DestFile, $myLine)
    WEnd

    FileClose($file)
    FileClose($DestFile)
EndFunc

Func TimeFix($MyTime)
    $timePart = StringSplit($MyTime, ":")
    Return $timePart[1] & ":" & $timePart[2]
EndFunc   ;==>TimeFix

send.txt

Share this post


Link to post
Share on other sites



Give this a test run:

#Tidy_Parameters=/tc 0 /kv 0 /reel
#AutoIt3Wrapper_Au3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6

#include <Date.au3>
#include <Array.au3>

Global Const $pipe = Chr(124)
Global Const $asterisk = Chr(42)

Global $MyDate
Global $MyTime

Global Const $TempDate = _DateAdd('D', -1, _NowCalc())

_DateTimeSplit($TempDate, $MyDate, $MyTime)

Global Const $FolderMonth = StringUpper(_DateToMonth($MyDate[2]))
Global Const $FolderYear = $MyDate[1]
Global Const $FileYear = StringRight($MyDate[1], 4)

Global $FileMonth

If $MyDate[2] < 10 Then
    $FileMonth = "0" & $MyDate[2]
Else
    $FileMonth = $MyDate[2]
EndIf

Global $FileDay

If $MyDate[3] < 10 Then
    $FileDay = "0" & $MyDate[3]
Else
    $FileDay = $MyDate[3]
EndIf

Global Const $FileDate = $FileMonth & "-" & $FileDay & "-" & $FileYear

ConvertFile()

Func ConvertFile()
    $file = FileOpen(@ScriptDir & "\send.txt", 0)

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

    $DestFile = FileOpen(@ScriptDir & "\Send-" & $FileDate & ".txt", 10)

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

    $line = FileReadLine($file);This will skip the header row

    While 1
        $line = FileReadLine($file)

        If @error = -1 Then ExitLoop

        $splitLine = StringSplit($line, @TAB)

        If @error Then
            $splitLine = StringSplit($line, @CRLF)
        EndIf

        Global $my[35]

        $my[0] = $splitLine[1]
        $my[1] = TimeFix($splitLine[2])
        $my[2] = $splitLine[3]
        $my[3] = $splitLine[4]
        $my[4] = $splitLine[5]
        $my[5] = $splitLine[6]
        $my[6] = $splitLine[7]
        $my[7] = $splitLine[8]
        $my[8] = $splitLine[9]
        $my[9] = $splitLine[10]
        $my[10] = $splitLine[11]
        $my[11] = $splitLine[12]
        $my[12] = $splitLine[13]
        $my[13] = TimeFix($splitLine[14])
        $my[14] = TimeFix($splitLine[15])
        $my[15] = TimeFix($splitLine[16])
        $my[16] = TimeFix($splitLine[17])
        $my[17] = TimeFix($splitLine[18])
        $my[18] = $splitLine[19]
        $my[19] = $splitLine[20]
        $my[20] = $splitLine[21]
        $my[21] = ""
        $my[22] = ""
        $my[23] = ""
        $my[24] = StringUpper($splitLine[23])
        $my[25] = StringUpper($splitLine[23])
        $my[26] = ""
        $my[27] = ""
        $my[28] = ""
        $my[29] = "";TimeFix($splitLine[23])
        $my[30] = $splitLine[5]
        $my[31] = ""
        $my[32] = ""
        $my[33] = ""
        $my[34] = $asterisk

        ; replaced with the following for loop
;~      $myLine = $my01 & $p & $my02 & $p & $my03 & $p & $my04 & $p & $my05 & $p & $my06 & $p & $my07 & $p & $my08 & $p & $my09 & $p & $my10 & $p & $my11 & $p & $my12 & $p & $my13 & $p & $my14 & $p & $my15 & $p & $my16 & $p & $my17 & $p & $my18 & $p & $my19 & $p & $my20 & $p & $my21 & $p & $my22 & $p & $my23 & $p & $my24 & $p & $my25 & $p & $my26 & $p & $my27 & $p & $my28 & $p & $my29 & $p & $my30 & $p & $my31 & $p & $my32 & $p & $my33 & $p & $my34 & $p & $my35

        Global $myline
        For $i = 0 To 34
            $myline &= $my[$i]

            If $i <> 34 Then
                $myline &= $pipe
            EndIf
        Next

        FileWriteLine($DestFile, $myLine)
    WEnd

    FileClose($file)

    FileClose($DestFile)
EndFunc   ;==>ConvertFile

Func TimeFix(Const $MyTime)
    Const $timePart = StringSplit($MyTime, ':')
    Return $timePart[1] & ':' & $timePart[2]
EndFunc   ;==>TimeFix

Share this post


Link to post
Share on other sites

Give this a test run:

#Tidy_Parameters=/tc 0 /kv 0 /reel
#AutoIt3Wrapper_Au3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6

#include <Date.au3>
#include <Array.au3>

Global Const $pipe = Chr(124)
Global Const $asterisk = Chr(42)

Global $MyDate
Global $MyTime

Global Const $TempDate = _DateAdd('D', -1, _NowCalc())

_DateTimeSplit($TempDate, $MyDate, $MyTime)

Global Const $FolderMonth = StringUpper(_DateToMonth($MyDate[2]))
Global Const $FolderYear = $MyDate[1]
Global Const $FileYear = StringRight($MyDate[1], 4)

Global $FileMonth

If $MyDate[2] < 10 Then
    $FileMonth = "0" & $MyDate[2]
Else
    $FileMonth = $MyDate[2]
EndIf

Global $FileDay

If $MyDate[3] < 10 Then
    $FileDay = "0" & $MyDate[3]
Else
    $FileDay = $MyDate[3]
EndIf

Global Const $FileDate = $FileMonth & "-" & $FileDay & "-" & $FileYear

ConvertFile()

Func ConvertFile()
    $file = FileOpen(@ScriptDir & "\send.txt", 0)

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

    $DestFile = FileOpen(@ScriptDir & "\Send-" & $FileDate & ".txt", 10)

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

    $line = FileReadLine($file);This will skip the header row

    While 1
        $line = FileReadLine($file)

        If @error = -1 Then ExitLoop

        $splitLine = StringSplit($line, @TAB)

        If @error Then
            $splitLine = StringSplit($line, @CRLF)
        EndIf

        Global $my[35]

        $my[0] = $splitLine[1]
        $my[1] = TimeFix($splitLine[2])
        $my[2] = $splitLine[3]
        $my[3] = $splitLine[4]
        $my[4] = $splitLine[5]
        $my[5] = $splitLine[6]
        $my[6] = $splitLine[7]
        $my[7] = $splitLine[8]
        $my[8] = $splitLine[9]
        $my[9] = $splitLine[10]
        $my[10] = $splitLine[11]
        $my[11] = $splitLine[12]
        $my[12] = $splitLine[13]
        $my[13] = TimeFix($splitLine[14])
        $my[14] = TimeFix($splitLine[15])
        $my[15] = TimeFix($splitLine[16])
        $my[16] = TimeFix($splitLine[17])
        $my[17] = TimeFix($splitLine[18])
        $my[18] = $splitLine[19]
        $my[19] = $splitLine[20]
        $my[20] = $splitLine[21]
        $my[21] = ""
        $my[22] = ""
        $my[23] = ""
        $my[24] = StringUpper($splitLine[23])
        $my[25] = StringUpper($splitLine[23])
        $my[26] = ""
        $my[27] = ""
        $my[28] = ""
        $my[29] = "";TimeFix($splitLine[23])
        $my[30] = $splitLine[5]
        $my[31] = ""
        $my[32] = ""
        $my[33] = ""
        $my[34] = $asterisk

        ; replaced with the following for loop
;~      $myLine = $my01 & $p & $my02 & $p & $my03 & $p & $my04 & $p & $my05 & $p & $my06 & $p & $my07 & $p & $my08 & $p & $my09 & $p & $my10 & $p & $my11 & $p & $my12 & $p & $my13 & $p & $my14 & $p & $my15 & $p & $my16 & $p & $my17 & $p & $my18 & $p & $my19 & $p & $my20 & $p & $my21 & $p & $my22 & $p & $my23 & $p & $my24 & $p & $my25 & $p & $my26 & $p & $my27 & $p & $my28 & $p & $my29 & $p & $my30 & $p & $my31 & $p & $my32 & $p & $my33 & $p & $my34 & $p & $my35

        Global $myline
        For $i = 0 To 34
            $myline &= $my[$i]

            If $i <> 34 Then
                $myline &= $pipe
            EndIf
        Next

        FileWriteLine($DestFile, $myLine)
    WEnd

    FileClose($file)

    FileClose($DestFile)
EndFunc   ;==>ConvertFile

Func TimeFix(Const $MyTime)
    Const $timePart = StringSplit($MyTime, ':')
    Return $timePart[1] & ':' & $timePart[2]
EndFunc   ;==>TimeFix

It appears to have the same problem min original script had. FileReadLine stops when it encounters the first CRLF. Some of the "lines" contain a CRLF before the "line" actually ends. Would FileRead be a possible solution?

Share this post


Link to post
Share on other sites

Could you post an example of the file, where Field23 contains the problem content?

Or just that field?

William

Share this post


Link to post
Share on other sites

Could you post an example of the file, where Field23 contains the problem content?

Or just that field?

William

I attached a copy of the file to my first post. It is send.txt

Share this post


Link to post
Share on other sites

I attached a copy of the file to my first post. It is send.txt

Sorry - the way it's tabulated made me think Field23 was blank - but I take it that all the content thereafter is the content of Field23?

W.

Share this post


Link to post
Share on other sites

Field 23 is the last field on each line and contains a name. The first line of the text file has "header" information. In the first line of actual data Field 23 should contain "Arbuckle, Jon, ABCD CRLF Shultz, Charles, ABCD" However the way FileReadLine functions it ends the line at "Arbuckle, Jon, ABCD. FileReadLine then treats Shultz, Charles, ABCD" as a new line. The CRLF that separates the two names could be replaced with another character. I don't need to maintain that CRLF. As a work around I have been manually changing the CRLF between the names to a semicolon in the source file so my script would continue to work, but I files that contain hundreds of records this has become very time consuming.

So the sample source file contains 3 records of data. The first has two names in Field23. The second and third record have only one name in Field23.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This example shows just returning the formatted data.

You'd have to replace the quotes yourself in another routine or something, this was just to be universally used.

Global $s_test_file = "send.txt"
Global $s_formatted = _FormatTextFile($s_test_file, "; ")

If @error Then
    ConsoleWrite("+! Error: " & @error & @CRLF)
Else
    ConsoleWrite($s_formatted & @CRLF)
EndIf

Func _FormatTextFile($s_infile, $s_sep = " ", $f_writetofile = False, $s_outfile = -1, $s_delim = @TAB)

    If Not FileExists($s_infile) Then Return SetError(1, 0, 0)

    Local $s_read = FileRead($s_infile)
    If $s_read = "" Then Return SetError(2, 0, 0)

    ; We will be working with individual lines, split file for it
    Local $a_split = StringSplit(StringStripCR($s_read), @LF)

    ; Get header delim count
    Local $s_header = $a_split[1]
    Local $a_header = StringSplit($s_header, $s_delim, 1)
    Local $i_header_count = $a_header[0]

    Local $a_delim, $s_out_data

    ; Work in reverse, last line to work with is the one after the header
    For $iline = $a_split[0] To 2 Step -1
        ; We only want lines with data, sometimes last line has no data
        If $a_split[$iline] = "" Then ContinueLoop

        ; Get lines delim count
        $a_delim = StringSplit($a_split[$iline], $s_delim, 1)
        If $a_delim[0] <> $i_header_count Then
            $s_out_data = $s_sep & $a_split[$iline] & $s_out_data
        Else
            $s_out_data = @CRLF & $a_split[$iline] & $s_out_data
        EndIf
    Next

    ; If the first two characters are not CRLF there was an error in data
    If StringLeft($s_out_data, 2) <> @CRLF Then Return SetError(3, 0, 0)

    ; Add header back to string
    $s_out_data = $s_header & $s_out_data

    ; If we are not writing to data, then we just want the raw text that was fixed
    If Not $f_writetofile Then Return $s_out_data

    ; If we don't have an out file, then we are replacing original file
    ;  will over write original file by default
    If $s_outfile = -1 Or $s_outfile = Default Then $s_outfile = $s_infile

    ; Write data to file
    Local $h_open = FileOpen($s_outfile, 2)
    FileWrite($h_open, $s_out_data)
    FileClose($h_open)

    Return 1
EndFunc

To change the original file, the 3rd parameter would be "True", the 4th parameter would be Default or -1 if you wanted to change the original file, or the name of the file you'd like to write the formatted data to.

Warning, if you are writing to a file, and you leave it as default, it will over write the original file if it can.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

This should work, just so long as:

no line in the destination file is the same as any other line

the line that is segmented always belongs to the prior Field23

While 1
        $Line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $SplitLine = StringSplit($Line, @TAB)
        
        If UBound($SplitLine) <> 24 Then    ;Current line is a segment if the Array does not have 24 elements (counter + 23 elements = 24)
            Local $oString = FileRead($DestFile)    ;read existing Text into a variable
            Local $LastLine = FileReadLine($DestFile, -1)   ;read the last line of Destination into a variable
            Local $JoinedLine = $LastLine & $Line   ;append the last line of the Destination File with the current line segment
            Local $oFile = FileOpen($DestFile, 2)   ;Open Destination File for writing and clear contents
            $oString = StringReplace($oString, $LastLine, $JoinedLine)  ;replace last line with joined lines
            FileWrite($oFile, $oString) ;write modified text to Destination File
            FileClose($oFile)   ;close handle to Destination File
            ContinueLoop    ;Go to next line of send.txt
        EndIf
        
        $my01 = $SplitLine[1]
        $my02 = TimeFix($SplitLine[2])
        $my03 = $SplitLine[3]
        $my04 = $SplitLine[4]
        $my05 = $SplitLine[5]
        $my06 = $SplitLine[6]
        $my07 = $SplitLine[7]
        $my08 = $SplitLine[8]
        $my09 = $SplitLine[9]
        $my10 = $SplitLine[10]
        $my11 = $SplitLine[11]
        $my12 = $SplitLine[12]
        $my13 = $SplitLine[13]
        $my14 = TimeFix($SplitLine[14])
        $my15 = TimeFix($SplitLine[15])
        $my16 = TimeFix($SplitLine[16])
        $my17 = TimeFix($SplitLine[17])
        $my18 = TimeFix($SplitLine[18])
        $my19 = $SplitLine[19]
        $my20 = $SplitLine[20]
        $my21 = $SplitLine[21]
        $my22 = ""
        $my23 = ""
        $my24 = ""
        $my25 = StringUpper($SplitLine[23])
        $my26 = StringUpper($SplitLine[23])
        $my27 = ""
        $my28 = ""
        $my29 = ""
        $my30 = "";TimeFix($splitLine[23])
        $my31 = $SplitLine[5]
        $my32 = ""
        $my33 = ""
        $my34 = ""
        $my35 = $Asterisk

        $myLine = $my01 & $p & $my02 & $p & $my03 & $p & $my04 & $p & $my05 & $p & $my06 & $p & $my07 & $p & $my08 & $p & $my09 & $p & $my10 & $p & $my11 & $p & $my12 & $p & $my13 & $p & $my14 & $p & $my15 & $p & $my16 & $p & $my17 & $p & $my18 & $p & $my19 & $p & $my20 & $p & $my21 & $p & $my22 & $p & $my23 & $p & $my24 & $p & $my25 & $p & $my26 & $p & $my27 & $p & $my28 & $p & $my29 & $p & $my30 & $p & $my31 & $p & $my32 & $p & $my33 & $p & $my34 & $p & $my35

        FileWriteLine($DestFile, $myLine)
    WEnd

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Actually, my previous script did not combine the fields properly. Try this script (you can comment out the _ArrayDisplay lines if this solves the problem for you). The second _ArrayDisplay is the result for which (I assume) you are looking.

#include <Array.au3>
#include <String.au3>
#include <Date.au3>

$p = Chr(124)

$Asterisk = Chr(42)

Dim $MyDate
Dim $MyTime

$TempDate = _DateAdd('D', -1, _NowCalc())
_DateTimeSplit($TempDate, $MyDate, $MyTime)
$FolderMonth = StringUpper(_DateToMonth($MyDate[2]))
$FolderYear = $MyDate[1]
$FileYear = StringRight($MyDate[1], 4)
If $MyDate[2] < 10 Then
    $FileMonth = "0" & $MyDate[2]
Else
    $FileMonth = $MyDate[2]
EndIf
If $MyDate[3] < 10 Then
    $FileDay = "0" & $MyDate[3]
Else
    $FileDay = $MyDate[3]
EndIf

Global $FileDate = $FileMonth & "-" & $FileDay & "-" & $FileYear


ConvertFile()

Func ConvertFile()
    $file = FileOpen(@ScriptDir & "\send.txt", 0)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    $DestFile = FileOpen(@ScriptDir & "\Send-" & $FileDate & ".txt", 10)
    If $DestFile = -1 Then
        MsgBox(0, "Error", "Unable to open destination file.")
        Exit
    EndIf
    Local $sArray = _StringExplode(FileRead(@ScriptDir & "\send.txt"), @CRLF)
    If IsArray($sArray) Then _ArrayDisplay($sArray)
    
    Local $oArray[UBound($sArray)][35]
    Local $Counter = 0
    
    For $i = 1 to UBound($sArray) - 1
        $Line = $sArray[$i]
        If Not $Line Then ExitLoop
        $SplitLine = StringSplit($Line, @TAB)
        
        If UBound($SplitLine) <> 24 Then ;Current line is a segment if the Array does not have 24 elements (counter + 23 elements = 24)
            $oArray[$Counter - 1][24] = StringUpper($oArray[$Counter - 1][24] & $Line)
            $oArray[$Counter - 1][25] = StringUpper($oArray[$Counter - 1][25] & $Line)
            ContinueLoop
        EndIf
    
        $oArray[$Counter][00] = $SplitLine[1]
        $oArray[$Counter][01] = TimeFix($SplitLine[2])
        $oArray[$Counter][02] = $SplitLine[3]
        $oArray[$Counter][03] = $SplitLine[4]
        $oArray[$Counter][04] = $SplitLine[5]
        $oArray[$Counter][05] = $SplitLine[6]
        $oArray[$Counter][06] = $SplitLine[7]
        $oArray[$Counter][07] = $SplitLine[8]
        $oArray[$Counter][08] = $SplitLine[9]
        $oArray[$Counter][09] = $SplitLine[10]
        $oArray[$Counter][10] = $SplitLine[11]
        $oArray[$Counter][11] = $SplitLine[12]
        $oArray[$Counter][12] = $SplitLine[13]
        $oArray[$Counter][13] = TimeFix($SplitLine[14])
        $oArray[$Counter][14] = TimeFix($SplitLine[15])
        $oArray[$Counter][15] = TimeFix($SplitLine[16])
        $oArray[$Counter][16] = TimeFix($SplitLine[17])
        $oArray[$Counter][17] = TimeFix($SplitLine[18])
        $oArray[$Counter][18] = $SplitLine[19]
        $oArray[$Counter][19] = $SplitLine[20]
        $oArray[$Counter][20] = $SplitLine[21]
        $oArray[$Counter][21] = ""
        $oArray[$Counter][22] = ""
        $oArray[$Counter][23] = ""
        $oArray[$Counter][24] = StringUpper($SplitLine[23])
        $oArray[$Counter][25] = StringUpper($SplitLine[23])
        $oArray[$Counter][26] = ""
        $oArray[$Counter][27] = ""
        $oArray[$Counter][28] = ""
        $oArray[$Counter][29] = "";TimeFix($splitLine[23])
        $oArray[$Counter][30] = $SplitLine[5]
        $oArray[$Counter][31] = ""
        $oArray[$Counter][32] = ""
        $oArray[$Counter][33] = ""
        $oArray[$Counter][34] = $Asterisk
        $Counter += 1
    Next
    FileClose($file)
    If IsArray($oArray) Then _ArrayDisplay($oArray)
    For $i = 0 To UBound($oArray, 1) - 1
        If Not $oArray[$i][0] Then ContinueLoop
        Local $myLine = ""
        For $z = 0 To UBound($oArray, 2) - 1
            $myLine &= $oArray[$i][$z]
            If $z <> UBound($oArray, 2) - 1 Then $myLine &= $p
        Next
        FileWriteLine($DestFile, $myLine)
    Next
    FileClose($DestFile)
    
EndFunc   ;==>ConvertFile

Func TimeFix($MyTime)
    $TimePart = StringSplit($MyTime, ":")
    Return $TimePart[1] & ":" & $TimePart[2]
EndFunc   ;==>TimeFix

Edited by Varian

Share this post


Link to post
Share on other sites

Actually, my previous script did not combine the fields properly. Try this script (you can comment out the _ArrayDisplay lines if this solves the problem for you). The second _ArrayDisplay is the result for which (I assume) you are looking.

#include <Array.au3>
#include <String.au3>
#include <Date.au3>

$p = Chr(124)

$Asterisk = Chr(42)

Dim $MyDate
Dim $MyTime

$TempDate = _DateAdd('D', -1, _NowCalc())
_DateTimeSplit($TempDate, $MyDate, $MyTime)
$FolderMonth = StringUpper(_DateToMonth($MyDate[2]))
$FolderYear = $MyDate[1]
$FileYear = StringRight($MyDate[1], 4)
If $MyDate[2] < 10 Then
    $FileMonth = "0" & $MyDate[2]
Else
    $FileMonth = $MyDate[2]
EndIf
If $MyDate[3] < 10 Then
    $FileDay = "0" & $MyDate[3]
Else
    $FileDay = $MyDate[3]
EndIf

Global $FileDate = $FileMonth & "-" & $FileDay & "-" & $FileYear


ConvertFile()

Func ConvertFile()
    $file = FileOpen(@ScriptDir & "\send.txt", 0)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    $DestFile = FileOpen(@ScriptDir & "\Send-" & $FileDate & ".txt", 10)
    If $DestFile = -1 Then
        MsgBox(0, "Error", "Unable to open destination file.")
        Exit
    EndIf
    Local $sArray = _StringExplode(FileRead(@ScriptDir & "\send.txt"), @CRLF)
    If IsArray($sArray) Then _ArrayDisplay($sArray)
    
    Local $oArray[UBound($sArray)][35]
    Local $Counter = 0
    
    For $i = 1 to UBound($sArray) - 1
        $Line = $sArray[$i]
        If Not $Line Then ExitLoop
        $SplitLine = StringSplit($Line, @TAB)
        
        If UBound($SplitLine) <> 24 Then ;Current line is a segment if the Array does not have 24 elements (counter + 23 elements = 24)
            $oArray[$Counter - 1][24] = StringUpper($oArray[$Counter - 1][24] & $Line)
            $oArray[$Counter - 1][25] = StringUpper($oArray[$Counter - 1][25] & $Line)
            ContinueLoop
        EndIf
    
        $oArray[$Counter][00] = $SplitLine[1]
        $oArray[$Counter][01] = TimeFix($SplitLine[2])
        $oArray[$Counter][02] = $SplitLine[3]
        $oArray[$Counter][03] = $SplitLine[4]
        $oArray[$Counter][04] = $SplitLine[5]
        $oArray[$Counter][05] = $SplitLine[6]
        $oArray[$Counter][06] = $SplitLine[7]
        $oArray[$Counter][07] = $SplitLine[8]
        $oArray[$Counter][08] = $SplitLine[9]
        $oArray[$Counter][09] = $SplitLine[10]
        $oArray[$Counter][10] = $SplitLine[11]
        $oArray[$Counter][11] = $SplitLine[12]
        $oArray[$Counter][12] = $SplitLine[13]
        $oArray[$Counter][13] = TimeFix($SplitLine[14])
        $oArray[$Counter][14] = TimeFix($SplitLine[15])
        $oArray[$Counter][15] = TimeFix($SplitLine[16])
        $oArray[$Counter][16] = TimeFix($SplitLine[17])
        $oArray[$Counter][17] = TimeFix($SplitLine[18])
        $oArray[$Counter][18] = $SplitLine[19]
        $oArray[$Counter][19] = $SplitLine[20]
        $oArray[$Counter][20] = $SplitLine[21]
        $oArray[$Counter][21] = ""
        $oArray[$Counter][22] = ""
        $oArray[$Counter][23] = ""
        $oArray[$Counter][24] = StringUpper($SplitLine[23])
        $oArray[$Counter][25] = StringUpper($SplitLine[23])
        $oArray[$Counter][26] = ""
        $oArray[$Counter][27] = ""
        $oArray[$Counter][28] = ""
        $oArray[$Counter][29] = "";TimeFix($splitLine[23])
        $oArray[$Counter][30] = $SplitLine[5]
        $oArray[$Counter][31] = ""
        $oArray[$Counter][32] = ""
        $oArray[$Counter][33] = ""
        $oArray[$Counter][34] = $Asterisk
        $Counter += 1
    Next
    FileClose($file)
    If IsArray($oArray) Then _ArrayDisplay($oArray)
    For $i = 0 To UBound($oArray, 1) - 1
        If Not $oArray[$i][0] Then ContinueLoop
        Local $myLine = ""
        For $z = 0 To UBound($oArray, 2) - 1
            $myLine &= $oArray[$i][$z]
            If $z <> UBound($oArray, 2) - 1 Then $myLine &= $p
        Next
        FileWriteLine($DestFile, $myLine)
    Next
    FileClose($DestFile)
    
EndFunc   ;==>ConvertFile

Func TimeFix($MyTime)
    $TimePart = StringSplit($MyTime, ":")
    Return $TimePart[1] & ":" & $TimePart[2]
EndFunc   ;==>TimeFix

You are awesome. It works like a charm. One question though...If I want to separate the two names with a semicolon is this the change I would make?

If UBound($SplitLine) <> 24 Then ;Current line is a segment if the Array does not have 24 elements (counter + 23 elements = 24)
            $oArray[$Counter - 1][24] = StringUpper($oArray[$Counter - 1][24] & ";" & $Line)
            $oArray[$Counter - 1][25] = StringUpper($oArray[$Counter - 1][25] & ";" & $Line)
            ContinueLoop
        EndIf

Share this post


Link to post
Share on other sites

You are awesome. It works like a charm. One question though...If I want to separate the two names with a semicolon is this the change I would make?

If UBound($SplitLine) <> 24 Then ;Current line is a segment if the Array does not have 24 elements (counter + 23 elements = 24)
            $oArray[$Counter - 1][24] = StringUpper($oArray[$Counter - 1][24] & ";" & $Line)
            $oArray[$Counter - 1][25] = StringUpper($oArray[$Counter - 1][25] & ";" & $Line)
            ContinueLoop
        EndIf

Yessiree...I thought you may eventually do something with those joined names! I could not find an efficient way of going back to the previous line with the Destination File having the handle opened, so it seemed quicker to put the results into an array.

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