Jump to content

Breaking Text into smaller files? Need help


Recommended Posts

So heres the problem at hand:

I have a large text file with say 50 000 lines. Lets call it Mylist.txt

I would like the script to take the first 500 lines or so, and create a Text file called Mylist1.txt and paste the 500 lines, create mylist2.txt with the next 500 lines, so on so forth until completion.

mylist1.txt, mylist2.txt

.... mylist[n].txt

Anyone know if its possible to do this? How to tackle?

Every bit of help is appreciated!

Link to comment
Share on other sites

Is it splitting the file that you want? Then yes it is possible and there are a lot of programs to do it. Check here for a standalone and portable example.

If you want it so you can read each file, then write your own!

Func _SplitFile ($sFile, $sOut, $nLine = 500)
   Local $hFile = FileOpen ($sFile, 0)
      If $hFile = -1 Then Return SetError (2 + 0 * FileClose ($hFile), 0, 0) ; File Non exisstant / Hidden or system
   Local $x = 1, $aLines
   If StringInStr (FileRead ($hFile), @LF) Then
      $aLines = StringSplit (StringStripCR (FileRead ($hFile)), @LF)
   Else
      $aLines = StringSplit (FileRead ($hFile), @CR)
   EndIf
   FileClose ($hFile)
   If $nLine < 0 Then Return SetError (2, 0, 0) ; Invalid $nLine Param.
   For $i = 1 to $aLines[0]
      If Mod ($i, $nLine) = 0 Then
         FileClose ($hFile)
         $x += 1
         $hFile = FileOpen ($sOut & $x & ".splt", 2)
      EndIf
      FileWrite ($hFile, $aLines[$i] & @CRLF)
   Next
   Return $x
EndFunc ; ==> _SplitFile

Easy enough, and reasonably paced too! I tried it on a 2000 line file, took 1 sec!

Edit: Code update. (and again)

Edited by mdiesel
Link to comment
Share on other sites

I appreciate the help man, but im not too sure on how the code is supposed to work. It aint working for me.

Is it supposed to spit out file1,file2.......file[n] with 500 lines each?

Edited by phatzilla
Link to comment
Share on other sites

Example: (run this, it will do the work. It writes the file, splits it, and writes it to seperate files. It will also bring up the folder where all this has happened afterwards)

<Snip!>

If You want to do your own, use

_SplitFile ( "File To split" , "File template to create" , Number of lines , "Extension to use" )

The file template to create is the start of the out files. If you put "...\out" you will get "...\out1.splt", "...\out2.splt" etc

Splt files are basic text files, I just renamed them so you know which ones are split. If you want text files then set the "Extension to use" param to "Txt" or ".txt".

MDiesel

Edit: Click here

Edited by mdiesel
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...