Jump to content

_FileWriteLine


FireFox
 Share

Recommended Posts

Hi,

I try to make _FileWriteLine udf for write to some lines instead of one for each function call :lmao:

This is my example not working with my Added lines :

_FileWriteLine('test.txt', '3|4|5', 'test')

Func _FileWriteLine($sFile, $iLine, $sText, $fOverWrite = 1)
    $sLine = StringSplit($iLine, '|') ;Added
    If $sLine[1] < 1 Then Return SetError(4, 0, 0) ;Added
    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)
    If $fil = -1 Then Return SetError(3, 0, 0)
    
    For $i = 1 To UBound($filtxt) - 1
        MsgBox(64, 'Current line', $i) ;Added
        For $Line = 1 To UBound($sLine) - 1 ;Added
            MsgBox(64, 'Line to write', $iLine) ;Added
            If $i = $sLine[$Line] Then ;Added
                If $fOverWrite = 1 Then
                    If $sText <> '' Then
                        FileWrite($fil, $sText & @CRLF)
                    Else
                        FileWrite($fil, $sText)
                    EndIf
                ElseIf $fOverWrite <> 1 Then
                    FileWrite($fil, $sText & @CRLF)
                    FileWrite($fil, $filtxt[$i] & @CRLF)
                EndIf
            ElseIf $i < UBound($filtxt, 1) - 1 Then
                FileWrite($fil, $filtxt[$i] & @CRLF)
            ElseIf $i = UBound($filtxt, 1) - 1 Then
                FileWrite($fil, $filtxt[$i])
            EndIf
        Next ;Added
    Next
    FileClose($fil)
    Return 1
EndFunc   ;==>_FileWriteLine

Place test.txt file in script dir

x
x
x
x
x

Only 3,4,5 line should be replaced with test... :)

Thanks for anyhelp.

Cheers, FireFox.

Link to comment
Share on other sites

@Andreik

:) I try to make an udf on it (see parts of my code is same as _FileWriteToLine) but I want to call it for change multiple lines instead of one...

Cheers, FireFox.

#include <File.au3>

Func _FileWriteLine($sFile,$iLine,$sText,$fOverWrite = 0)
    $SPLIT = StringSplit($iLine,"|")
    If IsArray($SPLIT) Then
        For $INDEX = 1 To $SPLIT[0]
            _FileWriteToLine($sFile,$SPLIT[$INDEX],$sText,$fOverWrite)
        Next
    EndIf
EndFunc
Edited by Andreik

When the words fail... music speaks.

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