Jump to content

How to paste between the lines of a txt file


Recommended Posts

I need to create an autoIT exe file which can copy a perticular line from a txt file say copy.txt & paste it to another file say paste.txt at the end of a perticular line saying VALUE at the begining of the line.

Right now I am able to copy & paste, but I can paste only at the begining of file but not at the specific location.

I am attaching the required files-test.au3, copy.txt & paste.txt.

Any AutoIT expert, Please help me.

test.au3

paste.txt

copy.txt

Link to comment
Share on other sites

  • Moderators

Many ways to do it, _FileReadToArray() / _FileCountLines() / etc... and so on... here's a way with StringRegExp() (soon to be broken I'm sure :lmao: )

$FileRead = FileRead('Copy.txt')
$aArray = StringRegExp($FileRead, '(?i:scm=)(.*?)\n', 3)
If IsArray($aArray) Then
    FileWriteLine('Paste.txt', _
        '       VALUE "' & StringTrimRight($aArray[0], 1) & '"')
EndIf

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

or..

look at parms for _FileWriteToLine

Best, randall

Yes, you are right.

We can give the line number to function _FileWriteToLine, but this line number differs everytime, it depends on a perticular string.

Is there any way to first search the perticular word (in my case it is "special build"), then get that line number & paste the text at the end of that line?

One more point, I want to clarify here is that the string may appear many times in the file & this line counter of occurance of the string in the file should be the last occurance.

Please guide me.

Link to comment
Share on other sites

Yes, you are right.

We can give the line number to function _FileWriteToLine, but this line number differs everytime, it depends on a perticular string.

Is there any way to first search the perticular word (in my case it is "special build"), then get that line number & paste the text at the end of that line?

One more point, I want to clarify here is that the string may appear many times in the file & this line counter of occurance of the string in the file should be the last occurance.

Please guide me.

Maybe modify this;

$filepasteread = FileRead("paste2.txt")
$i_Pos = StringInStr($filepasteread, "specialbuild", 0, -1)
$s_StartFile = StringLeft($filepasteread, $i_Pos)
StringReplace($s_StartFile, @LF, " ")
$numLines = @extended
$file = FileOpen("copy.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $check_rc = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($check_rc, "scm=") Then  _FileWriteToLine("paste2.txt", $numLines + 2, $check_rc, 0)
WEnd

FileClose($file)
Best, randall
Link to comment
Share on other sites

  • Moderators

Hmm, I don't get why you have to make it so bulky... _FileReadToArray() + StringInStr() + FileWriteLine() will work fine, as well as the option I gave.

Edit:

$FileRead = FileRead('Copy.txt')
$aArray = StringRegExp($FileRead, '(?i:scm=)(.*?)\n', 3)
If IsArray($aArray) Then
    For $iCC = 0 To UBound($aArray) - 1
        FileWriteLine('Paste.txt', _
            '       VALUE "' & StringTrimRight($aArray[$aArray], 1) & '"')
    Next
EndIfoÝ÷ ØØy©Ý²)©áÐuçZrÚ&jG¬º·­ébè¬iËnjYrnV§ØZ·*.j·°®+bhjwb~z-¶è­nV§«­¢+ØÀÌØí¥±Iô¥±I Ìäí
½Áä¹ÑáÐÌäì¤(ÀÌØíÉÉäôMÑÉ¥¹IáÀ ÀÌØí¥±I°Ìäì ý¤éÍ´ô¤ ¸¨ü¤ÀäÈí¸Ìäì°Ì¤)%%ÍÉÉä ÀÌØíÉÉä¤Q¡¸(%1½°ÀÌØíMÁ±¥ÐôMÑÉ¥¹MÁ±¥Ð¡MÑÉ¥¹MÑÉ¥Á
H¡¥±I ÌäíAÍѹÑáÐÌä줤°1¤(%%MÑÉ¥¹MÑÉ¥Á]L ÀÌØíMÁ±¥ÑlÀÌØíMÁ±¥ÑlÁut°à¤±ÐìÐìÌäìÌäìQ¡¸($%¥±]É¥Ñ1¥¹ ÌäíAÍѹÑáÐÌäì°
I1¤(%¹%(%½ÈÀÌØí¥
ôÄQ¼U    ½Õ¹ ÀÌØíÉÉ䤴Ä($%¥±]É¥Ñ1¥¹ ÌäíAÍѹÑáÐÌäì°|($$$ÌäìY1UÅÕ½ÐìÌäìµÀìMÑÉ¥¹QÉ¥µI¥¡Ð ÀÌØíÉÉålÀÌØíÉÉåt°Ä¤µÀìÌäìÅÕ½ÐìÌäì¤(%9áÐ)¹%
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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