Jump to content

Recommended Posts

Posted

Hello

I'm trying to use _FileWriteToLine to update a new line into a txt file, but I can't get it to write on the correct line, this is what I have tried:

Text file content is:

one
two
three
four

Attempt #1

#include <File.au3>

$FileLine = FileReadToArray("testy.txt")
$Lines = @extended
If IsArray($FileLine) Then

    For $i = 0 To $Lines-1
        If StringInStr($FileLine[$i], "three") Then
            _FileWriteToLine("testy.txt", $i, "three is 3", True)
            ExitLoop
        EndIf
    Next
EndIf

 

Attempt #2

#include <File.au3>

$FileLine = FileReadToArray("testy.txt")
$Lines = @extended
If IsArray($FileLine) Then

    For $i = 1 To $Lines Step 1
        If StringInStr($FileLine[$i], "three") Then
            _FileWriteToLine("testy.txt", $i, "three is 3", True)
            ExitLoop
        EndIf
    Next
EndIf

I always get this result: 

one
three is 3
three
four

Instead of: 

one
two
three is 3
four

 

Posted

If its always line 3 you can just use the function to write to the line without any read first.

You can also if you only need to check like 3 and not the whole file use FileReadLine() to read that specific line, and then write based on the results.

 

Just a more streamlined way to do it if the conditions you need to check have more specific parameters. 

Posted
1 minute ago, ViciousXUSMC said:

If its always line 3 you can just use the function to write to the line without any read first.

You can also if you only need to check like 3 and not the whole file use FileReadLine() to read that specific line, and then write based on the results.

 

Just a more streamlined way to do it if the conditions you need to check have more specific parameters. 

Thanks for the recommendation but the code is just an example, I need to loop through all lines to match the correct string using regex

Posted
On 10/29/2021 at 10:30 PM, Nine said:

Just rewrite the whole file (if it is not too too big).  Read it once, change with regexp, overwrite the whole thing...

This is also my thinking, could just update the array element and then use the _FileWriteFromArray() also currently stringinstr() is not the function to use if he needs RegEx.

 

However I thought if the file is large and its only like 3ish that needs to be read it could be pretty inefficient. 

 

The other bonus is he could write the new array to a totally different file and preserve the original as a backup of sorts.

  • 1 month later...
Posted

this should work ☺️

#include <File.au3>

$FileLine = FileReadToArray("testy.txt")
$Lines = @extended
If IsArray($FileLine) Then
    For $i = 1 To $Lines Step 1
       Local $path = StringInStr(FileReadLine("testy.txt",$i), "three")
       if $path  Then
            _FileWriteToLine("testy.txt", $i, "three is 3", True)
            ExitLoop
            EndIf
    Next
EndIf

 

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...