Jump to content

Add text to a .txt file...


Recommended Posts

Please help me do the following:

I want to find inside a txt file some words and then go back 2 lines and add some text.

The 2 words that i will look for, are not standard.

the first one begins with "A X" and ends with " "

and the second one begins with "A Y" and ends with " "

After finding the words, there will be added a text 2 lines before:

"word 1" for the 1st

and "word 2" for the 2nd.

Can you help on this?

Link to comment
Share on other sites

Here are the functions to need to read up on and test, best to test them standalone before using them together.

_FileCountLines()

FileReadLine()

FileWriteLine() or maybe _FileWriteToLine()

you might also want to look at _FileReadToArray()

They should get you started.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I suggest that you use _FileReadToArray() to put the Text file into an array. Then you can loop through the array and search each element for your text. When you find your text, use _ArrayInsert() to add the corresponding line to the count - 1 element. If you loop ascending, make sure that you check the count - 1 element to make sure that you it is not "word 1" or "word 2" before using _ArrayInsert(); otherwise, you may get stuck in a cyclic loop. Once you have read all of the elements in the array, you can write the array back to the file with FileOpen() & FileWrite() or _FileWriteFromArray()

EDIT: You'll need to Loop backwards in the array to get the desired results...Look at this example

#include <Array.au3>
#include <File.au3>

Local $Array
If Not _FileReadToArray(@WindowsDir & '\win.ini', $Array) Then
    Exit MsgBox(0, 'Fatal Error', 'Cannot Read File: ' & @WindowsDir & '\win.ini')
EndIf
_ArrayDisplay($Array, 'Initial "Win.ini" Contents')
For $i = $Array[0] To 1 Step -1
    Switch StringRegExp($Array[$i], '^\[')
        Case 1
            If $Array[$i - 1] <> '::::NEW SECTION:::' Then
                _ArrayInsert($Array, $i, '::::NEW SECTION:::')
                MsgBox(0, 'Found leading "["', 'Found Matching Text at line: ' & $i)
                $Array[0] = UBound($Array) - 1
            EndIf
    EndSwitch
Next
_ArrayDisplay($Array, 'Updated "Win.ini" Contents')

;Here I Would Write The New Array Back To The File
;_FileWriteFromArray(@WindowsDir & '\win.ini', $Array, 1)
Edited by Varian
Link to comment
Share on other sites

Thank you all very much!

Finally i used FileRead(), StringRegExp() and FileWriteToLine().

The only thing is that i had to "FileRead" two times,

while when i was inserting the new line (two lines above the found string)

the line counting was changed.

So before the second word search, i "FileRead" again.

Here is the code (i dont know if it can be written in shorter way).

Just works...

Happy new year everyone!

$aInput_Lines = StringSplit(FileRead("file.txt"), @CRLF, 1)

$Line=0

; Take each line in turn

For $i = 1 To $aInput_Lines[0]

_StringBetween($aInput_Lines[$i], "A", " "&" ")

if @error=0 Then

_FileWriteToLine("file.txt", $i+$Line-3, "word1")

$Line=$Line+1

EndIf

Next

$aInput_Lines = StringSplit(FileRead("file.txt"), @CRLF, 1)

$Line=0

; Take each line in turn

For $i = 1 To $aInput_Lines[0]

_StringBetween($aInput_Lines[$i], " Y", " ")

if @error=0 Then

_FileWriteToLine("file.txt", $i+$Line-3, "word2")

$Line=$Line+1

EndIf

Next

Edited by giannis121
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...