Jump to content

Copying text ranges to another text file


Recommended Posts

Hi

First of all, alot of this stuff is too technical for me even after looking through the help and examples so i am wondering if anybody can help me put my request together

What i want to do is as follows....

Copy a range of lines and columns to another range of lines and columns of a new text file, for example the source text file lines 15 to 19 with columns 22 to 24
Then do the same thing with different ranges of lines and columns but append them to the end of the first range (append to end of line, not new rows)

Repeat the process with new ranges until i am finished. Any range examples will do, i can easily adjust them

The output file would look something like...

Line61... somestuffhere CopiedStuffHere AppendedStuffHere MoreAppendedStuffHere ...
Line62... somestuffhere CopiedStuffHere AppendedStuffHere MoreAppendedStuffHere ...
Line63... somestuffhere CopiedStuffHere AppendedStuffHere MoreAppendedStuffHere ...
Line64... somestuffhere CopiedStuffHere AppendedStuffHere MoreAppendedStuffHere ...
Line65... somestuffhere CopiedStuffHere AppendedStuffHere MoreAppendedStuffHere ...

Is that possible ?

Link to post
Share on other sites
13 hours ago, justsomenoob said:

Is that possible ?

Yes.

See FileReadLine with line number to get a specific line.  See StringMid to read in a middle of a line.  The rest is just a loop...

Edited by Nine
Link to post
Share on other sites
7 minutes ago, Nine said:

Yes.

See FileRead with line number to get a specific line.  See StringMid to read in a middle of a line.  The rest is just a loop...

Ok sounds great but i don't know how to put it all together

I found this in another topic and modified it but it only does lines. No idea how to get the columns

$sFile = "D:\textin.txt"
$sRead = FileRead($sFile)

$aStrings = StringSplit($sRead, @CRLF, 1)

$sResult = ""

For $i = 15 To 19
    $sResult &= $aStrings[$i] & @CRLF
Next

$hFile = FileOpen ("D:\textout.txt", 2)
FileWrite($hFile, $sResult)
FileClose($hFile)

 

Link to post
Share on other sites
4 minutes ago, Subz said:

See _FileReadToArray and use the $sDelimiter flag, to specify how the columns are identified.

Sounds great, if only i knew how to put it all together. My ability is limited to manipulating an already working result

Anyone here to kindly put it all together ?

Link to post
Share on other sites
9 minutes ago, Subz said:

The helpfile has examples, its difficult to assist when we don't know what the format of the file you're reading is, i.e. is it tab delimited, comma etc...

It's just a text file with a bunch of text and other stuff. No special formatting or anything

Anyway i came up with this but the output still only captures the lines. Messagebox for columns is empty. I really don't know what i am doing

#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

$sFile = "D:\textin.txt"
$sRead = FileRead($sFile)

$aLines = StringSplit($sRead, @CRLF, 1)

$sResult = ""

For $i = 73 To 75
    $sResult &= $aLines[$i] & @CRLF
    $aColumns = StringMid($aLines, 10, 5)
Next

$hFile = FileOpen ("D:\textout.txt", 2)
FileWrite($hFile, $sResult)
FileClose($hFile)

MsgBox($MB_SYSTEMMODAL, "", $aColumns)

 

Link to post
Share on other sites

What are the columns you're referring to, please paste example of the source text and then how you want that text formatted.  When you mention columns I'm imaging a table  for example:

Row1Column1, Row1Column2, Row1Column3
Row2Column1, Row2Column2, Row2Column3
etc...

 

Link to post
Share on other sites
6 minutes ago, Subz said:

What are the columns you're referring to, please paste example of the source text and then how you want that text formatted.  When you mention columns I'm imaging a table  for example:

Row1Column1, Row1Column2, Row1Column3
Row2Column1, Row2Column2, Row2Column3
etc...

The source text is mostly words but it changes after a process and that process could be weeks or months away. All i need is to grab x amount of lines and x amount of columns to copy. As you can see in my last example i used lines 73 to 75 and columns 10 to 15 (10 to 15 was in the help example so i kept it). The range is whatever you wwant it to be, it's just where and how to place the right code, which is beyond my ability. Use any text file filled with letters or whatever and use any range to copy lines and columns. A working result is all i need, then i can change the lines and columns myself

Link to post
Share on other sites

Here with the FileReadLine method :

#include <Constants.au3>

Local $hFile = FileOpen("textin.txt")
Local $sResult, $sLine, $sColumn

For $i = 3 To 5
  $sLine = FileReadLine($hFile, $i)
  $sResult &= $sLine & @CRLF
  $sColumn &= StringMid($sLine, 10, 5) & @CRLF
Next

FileClose($hFile)

$hFile = FileOpen ("textout.txt", 2)
FileWrite($hFile, $sResult)
FileClose($hFile)

MsgBox($MB_SYSTEMMODAL, "", $sColumn)

 

Edited by Nine
Link to post
Share on other sites

I got it to write 3 lines and columns by rearranging a line and removing an &

Next step is to get it to write to a line and column instead of the first line and column, or just append to the end of the line

#include <Constants.au3>

Local $hFile = FileOpen ("D:\textin.txt")
Local $sResult, $sLine, $sColumn

For $i = 73 To 75
  $sLine = FileReadLine($hFile, $i)
  $sColumn &= StringMid($sLine, 14, 21) & @CRLF
  $sResult = $sColumn
Next

FileClose($hFile)

$hFile = FileOpen ("D:\textout.txt", 2)
FileWrite($hFile, $sResult)
FileClose($hFile)

MsgBox($MB_SYSTEMMODAL, "", $sColumn)

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...