geofromalimos Posted March 17, 2011 Posted March 17, 2011 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?
Moderators Melba23 Posted March 17, 2011 Moderators Posted March 17, 2011 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
geofromalimos Posted March 17, 2011 Author Posted March 17, 2011 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: expandcollapse popup#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!).
Moderators Melba23 Posted March 17, 2011 Moderators Posted March 17, 2011 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. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
geofromalimos Posted March 17, 2011 Author Posted March 17, 2011 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?
saywell Posted March 17, 2011 Posted March 17, 2011 (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 March 17, 2011 by saywell
geofromalimos Posted March 17, 2011 Author Posted March 17, 2011 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.WilliamWow! I didn't knew that StringSplit works with @CRLF!!! Thank you very much! I'll try it and let you know!!!!
Moderators Melba23 Posted March 17, 2011 Moderators Posted March 17, 2011 Hey man take it easy!Changing the question mid-thread is the one thing guaranteed to annoy me! 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 notNow: - "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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now