Jump to content

What would you use to split a txt file according to


Recommended Posts

I have a word list of a few thousands words/phrases each on one line.

What would be the approach to follow to get user input of splitting one file, say named "Word List.txt" to keep it simple, into however many text files happen to output from number of lines user requests?

Though it's not ideal, I do have a freeware that will do this but I already have a script that I invoke that I'd like to add this function to to keep it all in one process and to keep things simple. The freeware text-splitting utility is a bit cumbersome and requires entering all parameters each time you launch it and it's not as easy to remember output file naming to get exactly output filenames you need. If I could incorporate the file splitting function into this script that would be ideal.

So, to give an example, if user says to split "Word List.txt", in an example situation, into files with 100 lines each, I get 20 files labelled like this:

Word List1.txt01.Txt

Word List1.txt02.Txt

Word List1.txt03.Txt

Word List1.txt04.Txt

Word List1.txt05.Txt

Word List1.txt06.Txt

Word List1.txt07.Txt

Word List1.txt08.Txt

Word List1.txt09.Txt

Word List1.txt10.Txt

Word List1.txt11.Txt

Word List1.txt12.Txt

Word List1.txt13.Txt

Word List1.txt14.Txt

Word List1.txt15.Txt

Word List1.txt16.Txt

Word List1.txt17.Txt

Word List1.txt18.Txt

Word List1.txt19.Txt

Word List1.txt20.Txt

If I request the split be per 275 lines, I get 9 text files named similar to above containing 275 lines of the original source text file.

If it's easy to do in AutoIt, I'd prefer to automate the entire thing but wondering where to go in help file to do this?

Thanks! :)2

Link to comment
Share on other sites

So you say you have a freeware program that will do this for you already?

What I would do in your case, I would build my script around the freeware program

to control it and include it in my script with FileInstall() or alternatively call the program

and have the script wait until it closes.

Link to comment
Share on other sites

  • Moderators

Diana (Cda),

Here is a snippet which does what you want - over to you to integrate it into your script: :x

#include <File.au3>

Global $aLines

$sTitle = "WordList.txt"
_FileReadToArray($sTitle, $aLines)

$iSize = 225 ; This is the number of lines per file set by your user <<<<<<<<<<<<<<<<<<<<<<<<<<<

; Determine how many files we are going to create - $aLines[0] is the number of lines in the file
$iNumFiles = Ceiling($aLines[0] / $iSize)
; Determine number of padding zeros we need in the file titles
$iNumLen = StringLen($iNumFiles)
; Set the count
$iCount = 1

For $i = 1 To $iNumFiles
    ; Create a file with the correct title in sequence
    $hFile = FileOpen($sTitle & StringFormat( "%0" & $iNumlen & "i", $i) & ".txt", 2)
    ; Write the correct number of lines to the file
    For $j = $iCount To $iCount + $iSize - 1
        If $j > $aLines[0] Then ExitLoop
        FileWriteLine($hFile, $aLines[$j])
    Next
    ; Close the file
    FileClose($hFile)
    ; Set the correct line count for the next file
    $iCount = $j
Next

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

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