Sign in to follow this  
Followers 0
geofromalimos

Text file splitting

8 posts in this topic

Hi to all. Let's say we have a text file with this content:

Line1
Line2
Line3
Line4

Line5
Line6







Line7
Line8


Line9
Line10






Line11
Line12

What I am trying to achieve is to split the file and create a new one when 7 new lines are found. So the result of the above txt file

will be:

txt1.txt:

Line1
Line2
Line3
Line4

Line5
Line6

txt2.txt:

Line7
Line8


Line9
Line10

and txt3.txt:

Line11
Line12

Any ideas?

Share this post


Link to post
Share on other sites



geofromalimos,

Get the original file into an array with _FileReadToArray and then just run down the elements, skipping the lines you do not want while writing those you do to a suitable file. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

geofromalimos,

Get the original file into an array with _FileReadToArray and then just run down the elements, skipping the lines you do not want while writing those you do to a suitable file. :)

M23

I've done this but I'm brainly stuck! It's a bit complex. Let me show you some code:

#Include <File.au3>
#Include <array.au3>


;Parse all directories to an array
$Printer_Dir = _FileListToArray(@ScriptDir,"_*",2)

For $d = 1 To $Printer_Dir[0]

;Get Directory Size
$Printer_Dir_Size = DirGetSize(@ScriptDir&"\"&$Printer_Dir[$d])

;If the directory is empty bypass it
If $Printer_Dir_Size = 0 Then
    ConsoleWrite("Directory "&$Printer_Dir[$d]&" bypassed. Nothing received."&@LF)
EndIf

;If the directory is not empty then start reading files
If $Printer_Dir_Size > 0 Then

    ;Get all messages found inside a folder
    $Printer_Dir_Files = _FileListToArray(@ScriptDir&"/"&$Printer_Dir[$d],"TEXT_*")
    
    For $m = 1 to $Printer_Dir_Files[0]
        
        ;Get the file size
        $File_Size = FileGetSize(@ScriptDir&"/"&$Printer_Dir[$d]&"/"&$Printer_Dir_Files[$m])
        
        ;If the file size is 0 delete the file
        If $File_Size = 0 Then
            FileDelete(@ScriptDir&"/"&$Printer_Dir[$d]&"/"&$Printer_Dir_Files[$m])
            ConsoleWrite("File "&$Printer_Dir_Files[$m]&" has been deleted due to 0 file size."&@LF)
        EndIf

        ;If the file size is  not 0 start type a procedures
        If $File_Size > 0 Then
            
        ;HERE GOES THE CODE FOR SPLITTING!
        
        EndIf
        
    Next
    
EndIf
    
    
    
Next

As you can see the following things happen there:

1. Inside the script directory there various folders starting with _ e.g. _FOLDER1

2. Under each folder some files exist with names like TEXT_123131.txt

3. I scan everything and if something is found I want split it (if needed) and send it to a database(ok I know how to send it!).

Share this post


Link to post
Share on other sites

geofromalimos,

Where did the .txt files go? :)

If you do not explain what you actually want to do then you cannot complain when the answers do not meet the requirement. :)

But what is different? You have your file each time you go through the loop - so read it into an array as I suggested. Then instead of creating the mini .txt files with the extracted data, you send it to your database. The procedure is exactly the same - just the end product is altered. :P

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Hey man take it easy! I'm just asking for suggestions here, not for a ready project! I'm just trying to find a way of identifying the 7 blank lines. Any ideas?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Read the whole file into a string, and stringsplit with "@CRLF&@CRLF&@CRLF&@CRLF&@CRLF&@CRLF&@CRLF" as the delimiter.

Then write each array element back to the separate text files.

William

Edited by saywell

Share this post


Link to post
Share on other sites

Read the whole file into a string, and stringsplit with "@CRLF&@CRLF&@CRLF&@CRLF&@CRLF&@CRLF&@CRLF" as the delimiter.

Then write each array element back to the separate text files.

William

Wow! I didn't knew that StringSplit works with @CRLF!!! Thank you very much! I'll try it and let you know!!!!

Share this post


Link to post
Share on other sites

Hey man take it easy!

Changing the question mid-thread is the one thing guaranteed to annoy me! :P

And here you go again:

First post: - "when 7 new lines are found" - and the example .txt files seem to indicate 7 lines whether blank or not

Now: - "a way of identifying the 7 blank lines"

So which is it? :)

If all you want are blocks of seven lines then you merely take every successive seven elements of the array.

If you want only lines with text then you need to run through the array elements and check if they are empty, discarding those that are and counting those that are not until you reach 7.

If you want each block to begin with the next line containing text, then you take your seven lines, start looking at the following lines and discard every empty line until you find a non-empty one at which point you start your count of 7 again.

As I said before - ask a specific question and you will get plenty of help. If you could try to explain in a little more detail what you are trying to get from the data we can offer more pertinent advice. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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