Jump to content

Split file into 2 based on empty line


Recommended Posts

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

  • Similar Content

    • By lIlIIlIllIIIIlI
      $input = $CmdLine[1] $bytes = 1000000 ; 1000000 for 1 MB, 1000 for 1 KB $size = FileGetSize($input) $file = fileopen($input, 16) $max = ceiling($size / $bytes) for $i = 1 to $max $data = fileread($file, $bytes) $output = $input & '_' & $i & 'of' & $max filewrite($output, $data) next ^file split
      file path for a 20MB "video.mp4" is input, it will be output as "video.mp4_1of20", "video.mp4_2of20", etc. change $bytes to affect the split files size, this example made them 1MB (10^6 bytes)
       
      $input = $CmdLine[1] $name = stringtrimright($input, 1 + stringlen($input) - stringinstr($input, '_', 0, -1)) $split = stringsplit($input, 'of', 3) $max = $split[ubound($split) - 1] for $i = 1 to $max $in = $name & '_' & $i & 'of' & $max $file = fileopen($in, 16) $data = fileread($file) fileclose($file) filedelete($in) filewrite($name, $data) next ^ file join
      file path for any of the "video.mp4_Xof20" segments is input. the "video.mp4_Xof20" segments are read and written to "video.mp4" and then deleted, leaving the newly joined "video.mp4" in the end
       
      i made this today because i had a big file that i couldn't really open. i split it into 1000 chunks to make processing and stuff easier. i've also used it to split files to just under 25MB segments so i can attach and mail big files over gmail which is silly but it worked
×
×
  • Create New...