Sign in to follow this  
Followers 0
voidale

Trying to 8 lines from txt file to another txt file

43 posts in this topic

Hey guys trying to do this for few hours no luck,

what i want to do is open txt file copy the 8 first line delete it and delete the space,

so next time the 8 lines will be the 8-16 save the file.

create another txt file or open and paste it over there sounds easy right?

here is what i came up so far can you help?

; Script Start - Add your code below here

; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)

$answer = MsgBox(4, "first", "This script will run now. Run?")

; Check the user's answer to the prompt (see the help file for MsgBox return values)

; If "No" was clicked (7) then exit the script

If $answer = 7 Then

MsgBox(0, "AutoIt", "OK. Bye!")

Exit

EndIf

;Run txt

Do

$x=1 To 8

$ReadLine=FileReadLine("to.txt",$x)

ConsoleWrite($ReadLine & @CRLF)

ClipPut( $ReadLine & @CRLF)

Next

i don't have a clue if it works i can't see the txt file and how to copy it?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Not sure what you mean about the 8-16 but if you dont want to delete the $file then dont run the code with it in.

$file = "to.txt"

For $i = 1 To 8
    $read = FileReadLine($file,$i)
    ConsoleWrite($read & @CRLF)
Next    

FileDelete(($file)

EDIT: schoolboy error

Edited by JohnOne

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Look in the help file for _FileWriteToLine. In the remarks it says:

If _FileWriteToLine is called with $fOverWrite as 1 and $sText as "", it will delete the line.

Therefore if you set $iLine = 1, then it should delete the first line.

Share this post


Link to post
Share on other sites

Is this what you want?

#include <File.au3>
; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
$answer = MsgBox(4, "first", "This script will run now. Run?")

; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script

If $answer = 7 Then
    MsgBox(0, "AutoIt", "OK. Bye!")
    Exit
EndIf

;Run txt

For $x=1 To 8
    $ReadLine=FileReadLine("to.txt", 1) ; Read the first line.
    _FileWriteToLine("to.txt", 1, "", 1) ; Delete the first line.
    FileWriteLine("output.txt", $ReadLine) ; Append the line in the output file.
Next

By the way I noticed in your code that you have combined the terms 'Do' with 'Next'. They do not belong together like that. Loops using 'Do' must be terminated with the word 'Until' followed by an expression to trigger an escape from the loop.

Share this post


Link to post
Share on other sites

thanks guys for the help didn't expected to get replys so fast,

what i want to do is to Copy from that txt file the first 8 lines and past them over another txt file

now i want this to repeat so lets say i have 1000 lines over to.txt want it to copy from to.txt 8 lines

past over the other file close it all and then again repeat but to take the next 8 lines

so first time it takes 1-8 second time 8-16 and then 16-24 is it possible?

Share this post


Link to post
Share on other sites

For $x=1 To 8
    $ReadLine=FileReadLine("to.txt", 1) ; Read the first line.
    _FileWriteToLine("to.txt", 1, "", 1) ; Delete the first line.
Next

I was going to put something similar in, but could not figure if it would recount the lines after each write and fuff up somehow.

was thinking maybe 8 To 1 Step -1

I dont know how that would work.


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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

thanks guys for the help didn't expected to get replys so fast,

what i want to do is to Copy from that txt file the first 8 lines and past them over another txt file

now i want this to repeat so lets say i have 1000 lines over to.txt want it to copy from to.txt 8 lines

past over the other file close it all and then again repeat but to take the next 8 lines

so first time it takes 1-8 second time 8-16 and then 16-24 is it possible?

If you repeat the process you will just recreate an exact copy of the original file. I must be missing something. Perhaps you could explain a little more about how you want the output to appear.

Share this post


Link to post
Share on other sites

I was going to put something similar in, but could not figure if it would recount the lines after each write and fuff up somehow.

was thinking maybe 8 To 1 Step -1

I dont know how that would work.

I think an error may occur if you hit the end of the file before completing the process. If the number of lines is constant and divisable by 8 then there shouldn't be a problem. Otherwise some escape mechanism will have to be employed.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I cant really think of a reason for this myself

But maybe something like this

Local $Sourcefile = "from.txt"
Local $destinationfile = "to.txt"
Local $aFileLines[8]

For $i = 1 To 8
    $read = FileReadLine($Sourcefile,$i)
    $aFileLines[$i] = $read
Next

For $x=1 To 8
    _FileWriteToLine($Sourcefile, 1, "", 1)
Next

For $i = 1 To 8
    FileWriteLine($destinationfile,$aFileLines[$i])
Next

The count should always be 1 - 8 as the lines are getting deleted

Edited by JohnOne

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

If you repeat the process you will just recreate an exact copy of the original file. I must be missing something. Perhaps you could explain a little more about how you want the output to appear.

yes but in the newer txt file i will have 8 lines this is what i need get 8 lines from to.txt to another txt everytime

like this:

step 1

copy 1-8 lines from "to.txt"

step 2

past it over to "ftext.txt save & close

step 3 repeat now from 8-16 lines every repeat another 8 lines

i know it will be the same as the original but this is how i need it :D

Share this post


Link to post
Share on other sites

I cant really think of a reason for this myself

But maybe something like this

Local $Sourcefile = "from.txt"
Local $destinationfile = "to.txt"
Local $aFileLines[8]

For $i = 1 To 8
    $read = FileReadLine($Sourcefile,$i)
    $aFileLines[$i] = $read
Next

For $x=1 To 8
    _FileWriteToLine($Sourcefile, 1, "", 1)
Next

For $i = 1 To 8
    FileWriteLine($destinationfile,$aFileLines[$i])
Next

The count should always be 1 - 8 as the lines are getting deleted

This one looks like what i need but i get this error

"==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$aFileLines[$i] = $read

^ ERROR

>Exit code: 1 Time: 0.491

"

Share this post


Link to post
Share on other sites

I cant really think of a reason for this myself

But maybe something like this

Local $Sourcefile = "from.txt"
Local $destinationfile = "to.txt"
Local $aFileLines[8]

For $i = 1 To 8
    $read = FileReadLine($Sourcefile,$i)
    $aFileLines[$i] = $read
Next

For $x=1 To 8
    _FileWriteToLine($Sourcefile, 1, "", 1)
Next

For $i = 1 To 8
    FileWriteLine($destinationfile,$aFileLines[$i])
Next

The count should always be 1 - 8 as the lines are getting deleted

Array variable has incorrect number of subscripts.

Local $aFileLines[9]

Share this post


Link to post
Share on other sites

Never been hot on arrays

try that

Local $Sourcefile = "from.txt"
Local $destinationfile = "to.txt"
Local $aFileLines[8]
$aFileLines[0] = ""

For $i = 1 To 8
    $read = FileReadLine($Sourcefile,$i)
    $aFileLines[$i] = $read
Next

For $x=1 To 8
    _FileWriteToLine($Sourcefile, 1, "", 1)
Next

For $i = 1 To 8
    FileWriteLine($destinationfile,$aFileLines[$i])
Next

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Or

$aText = StringRegExp(FileRead("from.txt"), "(?i)(.+)(?:\v)+", 3)
If NOT @Error Then
    For $i = 0 To 7
        FileWriteLine("to.txt", $aText[$i])
    Next
EndIf
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Never been hot on arrays

try that

Local $Sourcefile = "from.txt"
Local $destinationfile = "to.txt"
Local $aFileLines[8]
$aFileLines[0] = ""

For $i = 1 To 8
    $read = FileReadLine($Sourcefile,$i)
    $aFileLines[$i] = $read
Next

For $x=1 To 8
    _FileWriteToLine($Sourcefile, 1, "", 1)
Next

For $i = 1 To 8
    FileWriteLine($destinationfile,$aFileLines[$i])
Next

new error

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$aFileLines[$i] = $read

^ ERROR

>Exit code: 1 Time: 0.227

Or

$aText = StringRegExp(FileRead("from.txt"), "(?i)(.+)(?:\v)+", 3)
If NOT @Error Then
    For $i = 0 To 7
        FileWriteLine("to.txt", $aText[$i])
    Next
EndIf

This one worked but the lines from from.txt, are 1 line over the to.txt gotta add enter?

and can you do it will delete the 8 that just copied from "from.txt" so if you repeat it the next 8 will be new lines

Edited by voidale

Share this post


Link to post
Share on other sites

yes but in the newer txt file i will have 8 lines this is what i need get 8 lines from to.txt to another txt everytime

like this:

step 1

copy 1-8 lines from "to.txt"

step 2

past it over to "ftext.txt save & close

step 3 repeat now from 8-16 lines every repeat another 8 lines

i know it will be the same as the original but this is how i need it :D

Is something else meant to happen after each set of 8 lines is transfered. Do you want to have multiple files each containing 8 lines of text? I'm confused.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Is something else meant to happen after each set of 8 lines is transfered. Do you want to have multiple files each containing 8 lines of text? I'm confused.

no just 2 txt files

copy each time 8 lines from "from.txt" to "to.txt

thats it

Cheers czardas

this one
Local $Sourcefile = "from.txt"
Local $destinationfile = "to.txt"
Local $aFileLines[9]


For $i = 1 To 8
    $read = FileReadLine($Sourcefile,$i)
    $aFileLines[$i] = $read
Next

For $x=1 To 8
    _FileWriteToLine($Sourcefile, 1, "", 1)
Next

For $i = 1 To 8
    FileWriteLine($destinationfile,$aFileLines[$i])
Next

gives this error, it seems _FileWriteToLine won't work to delete the lines tried to find another command to do that but nothing about it in the help file

_FileWriteToLine($Sourcefile, 1, "", 1)

^ ERROR

Edited by voidale

Share this post


Link to post
Share on other sites

Never been hot on arrays

You are forgetting that the first element of the array is index = 0. So you should declare the array with 9 elements, not 8. Alternatively you could use an array with 8 elements and use the expression For $i = 0 To 7 etc.

Share this post


Link to post
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
Sign in to follow this  
Followers 0