Sign in to follow this  
Followers 0
JimC

filewritetolinefromarray revisited

4 posts in this topic

The following function works great, but it seems to overwrite the exising lines in a file. What I need it to do is not woverwrite the line, but add the line to the file starting with line number x. What needs to be done to accomplish this ? Thanks in advance for any help you can provide.

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

Share this post


Link to post
Share on other sites



Pass the function a handle from FileOpen() with the "append" option and call FileClose() after the function.

Share this post


Link to post
Share on other sites

The problem is that I am not appending to the end of the file, but to a starting line number in the file.

Share this post


Link to post
Share on other sites

The problem is that I am not appending to the end of the file, but to a starting line number in the file.

There is no such thing as "file insert". You will have to read the old file, and write the new file. You could read the old file to a string or array, and then write the new file to the same location, but that will be either overwrite or append, there is no FileOpen() with "insert mode".

That could be simulated by reading x lines from the old file, writing them to the new file, appending your other lines, then writing the rest of the lines from the old file (or not, as required). But while you are writing to the new file, it will either be overwrite or append.

:)


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

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