Jump to content

Split file into 2 based on empty line


 Share

Recommended Posts

I have 1 file. There is one blank line at a random point in the file.

I need to split this file into two different files.

File #1 is all text above the blank line. File #2 is all text below the blank line.

Any help appreciated.

Link to comment
Share on other sites

  • Moderators

dplaut,

Read the file into an array - look for the blank line - write the new files from the array using that line index as the stop/start point for the elements written.

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

Now image the file you want to split is... the following script, which contains a single blank line in it.
Another solution could be to read your file into a single string (you'll have to add some error checking in the script)
Uncomment the 2 FileWrite() lines if you want to the 2 resulting files to be written in your script directory

#include <MsgBoxConstants.au3>
Example()

Func Example()
   $sContent = FileRead(@ScriptFullPath)
   $iEmpty_line = StringInStr($sContent, @CRLF & @CRLF) ; 4 characters = 1 empty line
   $sContent_left = StringLeft($sContent, $iEmpty_line + 1) ; +1 includes 1st @CRLF
   $sContent_right = StringMid($sContent, $iEmpty_line + 4) ; +4 bypass @CRLF & @CRLF
   MsgBox($MB_SYSTEMMODAL, "Before empty line", $sContent_left)
   MsgBox($MB_SYSTEMMODAL, "After empty line", $sContent_right)
   ; FileWrite(@ScriptDir & "\todelete1.txt", $sContent_left)
   ; FileWrite(@ScriptDir & "\todelete2.txt", $sContent_right)
EndFunc   ;==>Example

 

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

×
×
  • Create New...