Jump to content
Sign in to follow this  

_FileWriteToLine2, the _FileWriteToLine()'s reborn.

Recommended Posts



Last morning, I wanted to create a file from an array (using the "_FileWriteFormArray" function), and updating the line I wanted.

So, I found the "_FileWriteToLine" function, that wasn't exactly I needed.

Fixed :

-> _FileWriteToLine adds one more blank line when call for the last line of the file (My script is based on the number lines in a file..)

-> impossible to write a (single) blank first line

-> a "FileWrite()" is called for each line of the text file (Very slow.)

-> Bad use of "" / @CRLF to create (or set) a single blank line

-> (minor) "$i" is not set as Local scope

-> (minor) lot of "Ubound"s called, without change of their value

Func    _FileWriteToLine2 ($sFile, $iLine, $sText, $fOverWrite = 0)
    If  $iLine <= 0 Then    Return  SetError (4, 0, 0)
    If  Not IsString ($sText)   Then    Return  SetError (6, 0, 0)
    If  $fOverWrite <> 0    And $fOverWrite <> 1    Then    Return  SetError (5, 0, 0)
    If  Not FileExists ($sFile) Then    Return  SetError (2, 0, 0)

    Local   $filtxt = FileRead ($sFile, FileGetSize ($sFile))
    $filtxt = StringSplit ($filtxt, @CRLF, 1)
    If  UBound ($filtxt, 1) < $iLine    Then    Return  SetError (1, 0, 0)
    Local   $fil = FileOpen ($sFile, 2), $i, $limit = UBound ($filtxt) - 1, $file2write = ""
    If  $fil = -1   Then    Return  SetError (3, 0, 0)
    For $i = 1  To  $limit
        If  $i = $iLine Then
            If  $fOverWrite = 1 Then
                If  $sText <> ""    Then
                    If  $i < $limit And $sText <> @CRLF Then
                        $file2write &= $sText & @CRLF
                    ElseIf  $i = $limit Or  $sText = @CRLF  Then
                        $file2write &= $sText
                    If  $i < $limit Then
                        $file2write &= $sText
            If  $fOverWrite = 0 Then
                If  $i < $limit Then
                    If  $sText = @CRLF  Then
                        $file2write &= @CRLF & $filtxt[$i] & @CRLF
                        $file2write &= $sText & @CRLF & $filtxt[$i] & @CRLF
                ElseIf  $i = $limit Then
                    If  $sText = @CRLF  Then
                        $file2write &= @CRLF & $filtxt[$i]
                        $file2write &= $sText & @CRLF & $filtxt[$i]
        ElseIf  $i < $limit Then
            $file2write &= $filtxt[$i] & @CRLF
        ElseIf  $i = $limit Then
            $file2write &= $filtxt[$i]
    FileWrite ($fil, $file2write)
    FileClose ($fil)
    Return  1
EndFunc ;==>_FileWriteToLine2

For my own purpose, I changed some stuff : (changes not shown in the code above)

-> if the file is not found, it creates it without prompt (can be dangerous, I know)

-> if the line to write is > number of lines in the file to write, then it adds enough blanks lines (idem)

-> overwrite is not called only when its value equals 0. (instead of a difference between the $fOverWrite's values {"1", "0", "else"}, I use values {"0", "else"}.)

Talking about the future of the function :

-> Is all-in-one call of $filtxt a good idea ? (Directly slip the lines of the file, instead of reading it before (two -> one operation))

-> Change the "SetError(Var,0,0)" into "SetError(var)" ?

Thanks reading, I hope I did'nt bypass something important ? :)


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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.