Arlen Posted October 29, 2021 Share Posted October 29, 2021 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 Link to comment Share on other sites More sharing options...
Exit Posted October 29, 2021 Share Posted October 29, 2021 38 minutes ago, Arlen said: _FileWriteToLine("testy.txt", $i, "three is 3", True) _FileWriteToLine("testy.txt", $i+1, "three is 3", True) Arlen 1 App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted October 29, 2021 Share Posted October 29, 2021 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. Link to comment Share on other sites More sharing options...
Arlen Posted October 29, 2021 Author Share Posted October 29, 2021 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 Link to comment Share on other sites More sharing options...
Nine Posted October 30, 2021 Share Posted October 30, 2021 Just rewrite the whole file (if it is not too too big). Read it once, change with regexp, overwrite the whole thing... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted November 2, 2021 Share Posted November 2, 2021 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. Link to comment Share on other sites More sharing options...
ad777 Posted December 3, 2021 Share Posted December 3, 2021 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now