Jump to content

Move txt lines


hot202
 Share

Recommended Posts

Ok so what I would like to know how to do is

Move lines around in a .txt file or read from the txt and create a new .txt

I have a txt file that has about 200 lines looks like this

(line 1) 4642365 53765 8757 963794

(line 2) 8577478 85689 5855 585848

(line 3) 5856954 58477 3534 958468

(line 4) 6947847 58465 6777 756777

(line 5) 7476676 58457 6856 575576

(line 6) 5855764 57466 4746 584784

(line 7) 5635544 58647 5856 674676

(line 8) 5846746 58568 4747 574747

(line 9) 6747476 47474 5847 675856

(line 10) 5857568 58465 5847 575677

(line 11) 5746746 68457 6856 584755

(line 12) 4847558 47474 5758 584858

How would I move line 7 to line 2

Then line 2 to line 3 then line 8 to line 4. Do that for all the lines

Would it be easier to make a new txt out of it?

Edited by hot202
Link to comment
Share on other sites

  • Moderators

hot202,

Read the file into an array with.......wait for it....._FileReadToArray. :)

Then manipulate the array as you wish - if you have losts of changes to make, you might well find it easier to create a new array and copy the original elements into their new places.

Finally rewrite the array to a file with.....you guessed it....._FileWriteFromArray. But be careful of the [0] element. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

hot202,

What have you tried that has not worked? ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Then manipulate the array as you wish - if you have losts of changes to make, you might well find it easier to create a new array and copy the original elements into their new places.

It might also be interesting to check out _ArraySwap()

hth,

whim

Link to comment
Share on other sites

  • Moderators

hot202,

You know where we are if you run into problems. Have fun! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

this seems to work. Is it the right way to do it?

Im not sure about the For and the $NewLine is there a better way to do that?

#include <array.au3>
#Include <File.au3>
Dim $FileList
_FileReadToArray(@ScriptDir & "\PG5.txt",$FileList)

$Newline = 0
For $i = 47 To $FileList[0]
    $Newline = $Newline +2
    _ArrayInsert($FileList, $Newline, $FileList[$i])
    _ArrayDelete($FileList, $i + 1)
Next

_FileWriteFromArray(@ScriptDir & "\Test.txt", $FileList, 1)
Switch @Error
    Case 1
        MsgBox(1,"ERROR", "Error opening specified file")
    Case 2
        MsgBox(1,"ERROR", "Input is not an Array")
    Case 3
        MsgBox(1,"ERROR", "Error writing to file")
    case Else
        MsgBox(1,"DONE", "File Created")
EndSwitch
Link to comment
Share on other sites

  • Moderators

hot202,

Is it the right way to do it?

If it gets you what you want then the answer can only be "Yes!". ;)

is there a better way to do that?

As I am unsure exactly what you are trying to do, that is very difficult to say. But see answer 1 above! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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