large *.txt to many smaller *.txt Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Barney15
Posts: 168
Joined: 12 Jan 2020, 14:18

large *.txt to many smaller *.txt

12 Jun 2021, 09:55

created this script to split large *.txt to many smaller .*txt files contain equally given line numbers,
the issue i face is : the text files get saved containing the exact given number except for the first text file small_txt_[1].txt that contain more.
plz help.

Code: Select all

; #Warn
#NoEnv
#MaxMem 512
ListLines Off
SendMode Input
SetBatchLines -1
Process, Priority,, High
SetWorkingDir %A_ScriptDir%
F2::Reload
Esc::ExitApp
F1::
FileSelectFile, SelectedFile, 1, % A_WorkingDir, Open a file, Text Documents (*.txt; *.bat)
InputBox, b, Info, How many lines in each *.txt, show, 250, 150
FileRead, var, % SelectedFile
s := 0, n := 1
Loop, Parse, var, `n, `r
	{
        ToolTip, % s++ . "`n" . n, 0, 0
        if(s == b) {
            Sleep, 10
            s := 0, n++
        } else {
            FileAppend, % A_LoopField . "`n", % "small_txt_[" . n . "].txt"
        }
    }   MsgBox, All Done
User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: large *.txt to many smaller *.txt  Topic is solved

12 Jun 2021, 10:32

One problem is that when you reach your max, you don't write that line. The following should work.

Code: Select all

F1::
FileSelectFile, sel, 1, % A_WorkingDir, Open a file, Text Documents (*.txt; *.bat)
If (sel = "")
 Return
If !FileExist(sel) {
  MsgBox, 48, Error, File not found. Aborting.`n`n%sel%
  Return
} Else InputBox, b, Lines per file, How many lines in each *.txt,, 250, 150
If ErrorLevel
 Return
If b is not integer
 Return
FileRead, var, %sel%
n := 1
Loop, Parse, var, `n, `r
{
 FileAppend, %A_LoopField%`n, small_txt_[%n%].txt
 n := Mod(A_Index, b) ? n : n + 1
}
MsgBox, 64, Done, Done!
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, filipemb, haomingchen1998, matt101, Oblomov228, RussF and 264 guests