Jump to content

fast way to write an array to an existing file


JimC
 Share

Recommended Posts

I have text files that I need written to existing files I first read them into an array. The problem is that i determine what line to start writing to then write out one line at a time. Is there a function that you can write an array to a file by indicating the starting line.

Second ? is that I need to apend an array to one line is there a way to accomplish this ?

Thanks in advance for any help you can provide.

Link to comment
Share on other sites

  • Moderators

Quick and dirty (Not tested):

Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1)
    Local $sStringStore, $sRead, $sStartStore, $aSplit, $iCC, $nUBound = $nEndArray, $sEndStore
    If $nEndArray = -1 Then $nUBound = UBound($nEndArray) - 1
    Switch $nLineFile
        Case -1
            For $iCC = $nStartArray To $nUBound
                $sStringStore &= $avArray & @CRLF
            Next
            Return FileWrite($hFile, StringTrimRight($sStringStore, 2))
        Case Else
            If $nLineFile > 1 Then
                $sRead = FileRead($hFile)
                $aSplit = StringSplit(StringStripCR($sRead), @LF)
                For $iCC = 1 To $nLineFile - 1
                    $sStartStore &= $aSplit[$iCC] & @CRLF
                Next
                For $iCC = $nLineFile To $aSplit[0]
                    $sEndStore &= $aSplit[$iCC] & @CRLF
                Next
                $sEndStore = StringTrimRight($sEndStore, 2)
            Else
                FileClose(FileOpen($hFile, 2))
            EndIf
            For $iCC = $nStartArray To $nUBound
                $sStringStore &= $avArray[$iCC] & @CRLF
            Next
            Return FileWrite($hFile, $sStartStore & StringTrimRight($sStringStore, 2) & $sEndStore)
    EndSwitch
    Return SetError(1, 0, 0)
EndFunc

Edit:

Just realized I didn't write the rest of the file out after the insert :) Fixed above.

Edited by SmOke_N

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.

Link to comment
Share on other sites

Quick and dirty (Not tested):

Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1)
    Local $sStringStore, $sRead, $sStartStore, $aSplit, $iCC, $nUBound = $nEndArray, $sEndStore
    If $nEndArray = -1 Then $nUBound = UBound($nEndArray) - 1
    Switch $nLineFile
        Case -1
            For $iCC = $nStartArray To $nUBound
                $sStringStore &= $avArray & @CRLF
            Next
            Return FileWrite($hFile, StringTrimRight($sStringStore, 2))
        Case Else
            If $nLineFile > 1 Then
                $sRead = FileRead($hFile)
                $aSplit = StringSplit(StringStripCR($sRead), @LF)
                For $iCC = 1 To $nLineFile - 1
                    $sStartStore &= $aSplit[$iCC] & @CRLF
                Next
                For $iCC = $nLineFile To $aSplit[0]
                    $sEndStore &= $aSplit[$iCC] & @CRLF
                Next
                $sEndStore = StringTrimRight($sEndStore, 2)
            Else
                FileClose(FileOpen($hFile, 2))
            EndIf
            For $iCC = $nStartArray To $nUBound
                $sStringStore &= $avArray[$iCC] & @CRLF
            Next
            Return FileWrite($hFile, $sStartStore & StringTrimRight($sStringStore, 2) & $sEndStore)
    EndSwitch
    Return SetError(1, 0, 0)
EndFunc

Edit:

Just realized I didn't write the rest of the file out after the insert :) Fixed above.

Can you give a brief explanation of each argument ?

Thank You !!1

Link to comment
Share on other sites

  • Moderators

Can you give a brief explanation of each argument ?

Thank You !!1

Sorry, thought they were self explanitory:

Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1)

$hFile is the file location/name

$avArray is the array you want to add to the file

$nStartArray is where you want to start adding from the array you just sent

$nEndArray is where you want to end in the array ... -1 = the end of the array

$nLineFile is the line you want to insert the array at in the file

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.

Link to comment
Share on other sites

I am feeding the following:

file to add array

array to read to write to file

item in array to start from I am feeding 1

feeding -1 for end of array

I am feeding a variable which is the starting line number to start the write

unfortunately it is not working as of yet. I will continue to mess around with it.

Link to comment
Share on other sites

  • Moderators

I am feeding the following:

file to add array

array to read to write to file

item in array to start from I am feeding 1

feeding -1 for end of array

I am feeding a variable which is the starting line number to start the write

unfortunately it is not working as of yet. I will continue to mess around with it.

Had my Ubound pointing at a string and not an array, this has been tested now:
Global $aArray, $hMyFile = "mytest.txt"
$aArray = StringSplit("def,ghi", ",")
_FileWriteToLineFromArray($hMyFile, $aArray, 1, -1, 2)

Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1)
    Local $sStringStore, $sRead, $sStartStore, $aSplit, $iCC, $nUBound = $nEndArray, $sEndStore
    If $nEndArray = -1 Then $nUBound = UBound($avArray) - 1
    Switch $nLineFile
        Case -1
            For $iCC = $nStartArray To $nUBound
                $sStringStore &= $avArray & @CRLF
            Next
            Return FileWrite($hFile, StringTrimRight($sStringStore, 2))
        Case Else
            If $nLineFile > 1 Then
                $sRead = FileRead($hFile)
                $aSplit = StringSplit(StringStripCR($sRead), @LF)
                For $iCC = 1 To $nLineFile - 1
                    $sStartStore &= $aSplit[$iCC] & @CRLF
                Next
                For $iCC = $nLineFile To $aSplit[0]
                    $sEndStore &= $aSplit[$iCC] & @CRLF
                Next
                $sEndStore = StringTrimRight($sEndStore, 2)
            EndIf
            For $iCC = $nStartArray To $nUBound
                $sStringStore &= $avArray[$iCC] & @CRLF
            Next
            If $sEndStore = "" Then $sStringStore = StringTrimRight($sStringStore, 2)
            FileClose(FileOpen($hFile, 2))
            Return FileWrite($hFile, $sStartStore & $sStringStore & $sEndStore)
    EndSwitch
    Return SetError(1, 0, 0)
EndFunc

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.

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