Jump to content

Updating one field to output


Recommended Posts

I am reading in records from a file. I'm writing those records out to a different file. Sometimes I need to change one of the fields in the output records. How can I write the code to output the updated fields?

The write below should make it more clear.

#include <file.au3>
#include <array.au3>
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number
HotKeySet("{ESC}", "Set_Exit")

Dim  $Allfields[1], $hQBOffSetEntries, $hQBTrans, $hQBTransOut, $Dates, $sString, $NumberOfLines
Dim  $fResult = 1, $Found = "No"
Global $QBTrans = "C:\Program Files\QuickBooks Pro 2004\Test.iif"
Global $QBTransOut = "C:\Program Files\QuickBooks Pro 2004\TestOut.iif"
Global $QBOffSetEntries = "C:\Program Files\QuickBooks Pro 2004\OffSettingEntries.txt"
$sPattern = '([^",]*|"(?:[^"]|"")*")(?:,|\Z)'
    _NFileCountLines($QBOffSetEntries)
Global $Vendor[$NumberOfLines], $Account[$NumberOfLines]

    OpenFiles()
;ReadOffSetEntries()

While $fResult = 1
    $asFields = 1  ; Used for WriteRecords
    ReadRecord()
WEnd

Set_Exit()

Func ReadRecord()   ;  Read the transaction records
    While $fResult = 1
        Global $sString = FileReadLine($hQBTrans)               
            If @error = -1 Then 
                ;End of file reached
                ExitLoop
            EndIf
        $asFields = StringRegExp($sString, $sPattern, 3)
;       _ArrayDisplay($asFields, "Input Fields")                                                        
            If $asFields[0] = "TRNS" Then 
                    For $x = 2 to Ubound($Vendor) - 1
                        If StringInStr($asFields[5],$Vendor[$x]) > 0 then
;                           MsgBox(0,"","FOUND IT!!!")
                            MsgBox(0, "Found", '"' & $asFields[5] & '" was not found in the array.' & @LF & "Vendor - " & $Vendor[$x] & @lf & "$x - " & $x & @LF & "NumberOfLines - " & $NumberOfLines)
                            WriteRecords()
                            Global $sString = FileReadLine($hQBTrans)
                                If @error = -1 Then 
                                    ;End of file reached
                                ExitLoop
                                EndIf
                            $asFields = StringRegExp($sString, $sPattern, 3)    ;Input fields
                                If $asFields[0] = "SPL" Then 
                                    $asFields[4] = '"' & $Account[$x] & '"'
                                    _ArrayDisplay($asFields, "Input Fields")
                                    WriteRecords()
                                EndIf
                            ExitLoop
                        EndIf
                            MsgBox(0, "Not Found", '"' & $asFields[5] & '" was not found in the array.' & @LF & "Vendor - " & $Vendor[$x] & @lf & "$x - " & $x & @LF & "NumberOfLines - " & $NumberOfLines)
                    Next
                    ExitLoop
            EndIf
;           MsgBox(4096, "Record", "InputString - " & $sString & @LF & "Result - " &$fResult)
            WriteRecords()
    WEnd            
EndFunc

Func WriteRecords()
    ; write records out
        FileWriteLine($hQBTransOut, $sString & @CRLF)       ;This is where my question begins. 
                                                            ;Sometimes I need to write out exactly what was read in.  $sString holds what was read in.  This is fine.
                                                            ;Sometimes I need to replace part of the line that was read in.  As in line #49 above.
                                                            ;How can I have AutoIt update the $sString to write it out with the replaced values? 

EndFunc ;==>FormatWriteRecords

Func OpenFiles()
        If Not FileExists($QBTrans) Then 
        MsgBox(0, "Error", "File doesn't exist - " & $QBTrans)
        Exit
    EndIf
Global $hQBTrans = FileOpen($QBTrans, 0)
    If $hQBTrans = -1 Then
        MsgBox(0, "Error", "Unable to open - " & $QBTrans)
        Exit
    EndIf
Global $hQBTransOut = FileOpen($QBTransOut, 1)
    If $hQBTransOut = -1 Then
        MsgBox(0, "Error", "Unable to open - " & $QBTransOut & " file.")
        Exit
    EndIf
    If Not FileExists($QBOffSetEntries) Then 
        MsgBox(0, "Error", "File doesn't exist - " & $QBOffSetEntries)
        Exit
    EndIf
Global $hQBOffSetEntries = FileOpen($QBOffSetEntries, 0)
    If $hQBOffSetEntries = -1 Then
        MsgBox(0, "Error", "Unable to open - " & $QBOffSetEntries)
        Exit
    EndIf
EndFunc ;==> OpenFiles

Func _NFileCountLines($sFile)
  Local $AArray
  Local $Content
  Local $FileSize
  If Not FileExists($sFile) Then
     Return 0
  Else
     $FileSize = FileGetSize($sFile)
     $Content = FileRead($sFile, $FileSize)
     If NOT StringInStr($Content, @LF) Then
        Return 1
     Else
        $AArray = StringSplit($Content, @LF)
;   _ArrayDisplay($AArray, "$AArray")                                                       

$NumberOfLines = $AArray[0]
        Return $NumberOfLines
     EndIf
  EndIf
EndFunc  ;==> _NFileCountLines

Func Set_Exit()
    FileClose($QBTrans)
    FileClose($QBTransOut)
    FileClose($QBOffSetEntries)
   Exit
EndFunc ;==>Set_Exit

Thank you,

Docfxit

Link to comment
Share on other sites

Since you are already using StringRegExp to find the required changes, just use StringRegExpReplace() to make the change to $sString.

:)

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

Since you are already using StringRegExp to find the required changes, just use StringRegExpReplace() to make the change to $sString.

:)

Thank you for the reply.

When I find a key word from the vendor file "CHEVRON" in the array "$asFields[5]" which looks like this "CHEVRON 00203660 NEWHALL CA "

on the "TRNS" record I need to replace the entire array element "$asFields[4]" on the next record "SPL" with "Car/Truck Expense:Gas".

What is actually in "$asFields[4]" in the "SPL" record is "WF CREDIT ACCOUNT DEBIT OFFSET" so I don't think StringRegExpReplace() will work this time.

I have solved this problem by reading the next record and using:

$asFields[4] = '"' & $Account[$x] & '"'

To replace the proper field.

Thank you,

Docfxit

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...