Jump to content

copy/paste text at intervals


ipodfrik
 Share

Recommended Posts

I am total newbie with autoit, so your patience is appreciated.

I'm trying to see if it's possible to do with autoit.

Basically I have a text that needs to be multiplied from the 1st line all the way to 4th line,

each line from 2 to 4 containing the same text as line 1.

then from 5th line, the text that is on 5th line should be copied over to 6,7,8 lines.

then from 9th line, the text that is on 9th line should be copied over to 10,11,12 lines and etc.

the script should run until designated line.

for example:

Original text (numbers are line numbers):

1. good text

2.

3.

4.

5. better text

6.

7.

8.

9. best text

10.

11.

12.

Modified text after running script:

1. good text

2. good text

3. good text

4. good text

5. better text

6. better text

7. better text

8. better text

9. best text

10. best text

11. best text

12. best text

Any ideas on how to do it?

Link to comment
Share on other sites

This example may give you some ideas.

#include <Array.au3> ;For _ArrayDisplay() only.

Local $iDesignatedLine = 11 ; 20
Local $str = "line 1" & @CRLF & @CRLF & @CRLF & @CRLF & _
        "line 5" & @CRLF & @CRLF & @LF & @CRLF & _
        "line 9" & @CRLF & @CRLF & @CRLF & @CRLF & _
        "line 13" & @LF & @CRLF & @CRLF & @CRLF & "line 17"
Local $sRes = ""

; ---------- Place each line of text into each element of an array. -----------
;Local $aArray
;_FileReadToArray("FilePath_Name", $aArray)

; or
;Local $aArray = StringSplit(StringStripCR(FileRead("FilePath_Name")), @LF) ; File will split each line on @CRLF or @LF only.

; or
Local $aArray = StringSplit(StringStripCR($str), @LF) ; String will split on @CRLF or @LF only.
; -------------------------------------------------------------------------------

;_ArrayDisplay($aArray)

; ------------ For each line in the array write that line 4 times to a string, $sRes. ------------
If $iDesignatedLine > UBound($aArray) - 1 Then $iDesignatedLine = UBound($aArray) - 1 ; Error check

For $i = 1 To $iDesignatedLine ; UBound($aArray) - 1
    $sRes &= $aArray[$i - Mod($i - 1, 4)] & @LF ; "$sRes &= " is the same as  "$sRes = $sRes & "
Next
$sRes = StringTrimRight($sRes, 1)

;ConsoleWrite($sRes & @LF)
; ---- Write rest of file to string, $sRes. ------------
If $iDesignatedLine < UBound($aArray) - 1 Then
    For $i = $iDesignatedLine To UBound($aArray) - 1
        $sRes &= $aArray[$i] & @LF ; "$sRes &= " is the same as  "$sRes = $sRes & "
    Next
    $sRes = StringTrimRight($sRes, 1)
EndIf

ConsoleWrite($sRes & @LF)

; ----- Write to file -------------
;#cs
Local $file = FileOpen("test.txt", 2) ; 2 = Write mode (erase previous contents)

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

FileWrite("test.txt", $sRes)
FileClose($file)
ShellExecute("test.txt")
;#ce
Link to comment
Share on other sites

This uses FileReadToArray() and For..To loops also.

filea.txt

Line 1
Line 2
Line 3

#include <file.au3>
Local $FileA, $FileB

_FileReadToArray("filea.txt", $FileA)
$FileB = FileOpen("fileb.txt", 1)

For $x = 1 To $FileA[0]
    For $y = 1 To 4
        FileWriteLine($FileB, $FileA[$x])
    Next
Next

FileClose($FileB)

fileb.txt

Line 1
Line 1
Line 1
Line 1
Line 2
Line 2
Line 2
Line 2
Line 3
Line 3
Line 3
Line 3

Note that fileb.txt is created by the script, but you need to create a file named filea.txt.

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

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