Incremented text files with specified amount of lines

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MarkBlart
Posts: 5
Joined: 25 Sep 2022, 17:36

Incremented text files with specified amount of lines

Post by MarkBlart » 25 Sep 2022, 17:55

Hi, I have a script that splits a main text files lines randomly into other text files.
Each text file that is made will have a max of 98 lines of random text from the main file and also I want there to be no repeats.
I also want each new file name to be incremented (NewFile1, NewFile2, NewFile3, etc).
I am able to get the first text file made but there are repeats and I dont know how make the next files.
Here is my code so far any help will be appreciated :]

Code: Select all


#SingleInstance Force

Inc := 1

Guiw := 300
Guih := 80
GuiX := 2432
GuiY := 47

MainF := "C:\Users\" A_UserName "\OneDrive\Desktop\MainList.txt"
NewF := "C:\Users\" A_UserName "\OneDrive\Desktop\NewFile"

Loop, Read, % MainF
  numlines := A_Index
  
Gui, +AlwaysOnTop +ToolWindow
Gui, Color, 0x202020, 0x333333
Gui, font, cFEFAF3
Gui, Add, Button, x5 y40 w60 gRunN, Run
Gui, Add, Button, x75 y40 w60 gRefreshH, Reload
Gui, Add, Button, x145 y40 w60 gNoteX, NoteX
Gui, Add, Button, x215 y40 w60 gDelL, Delete
Gui, Show, w%Guiw% h%Guih% x%GuiX% y%GuiY%
return

DelL:
Loop, %numlines%
{
FileDelete, % NewF Inc ".txt"
Inc++
}

Inc := 1
MsgBox,,,Deleted!,1
return

RunN:
Loop, %numlines%
{
		Loop, Read, % NewF Inc ".txt"
		numlinesS := A_Index
			if (numLinesS < 98){
				Random, randomNumber, 1, %numlines%
				FileReadLine, TXTT, %MainF%, %randomNumber%
				FileAppend, %TXTT%`n, % NewF Inc ".txt"
			}else{
			}
	}
	
Reload
return

RefreshH:
	Reload
return

NoteX:
	Runwait, taskkill /im Notepad.exe /f
return

Gosub esc

esc::
GuiClose:
ExitApp


BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Incremented text files with specified amount of lines

Post by BoBo » 26 Sep 2022, 01:45

You can push all lines into an array. From there you pick each line randomly (to write it into a file) and remove its item from the array to prevent duplicate picks. HTH

Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Incremented text files with specified amount of lines

Post by Rohwedder » 26 Sep 2022, 02:04

Hallo,
try, untested:

Code: Select all

#SingleInstance Force
Inc := 1
Guiw := 300
Guih := 80
GuiX := 2432
GuiY := 47
MainF := "C:\Users\" A_UserName "\OneDrive\Desktop\MainList.txt"
NewF := "C:\Users\" A_UserName "\OneDrive\Desktop\NewFile"
Loop, Read, % MainF
	numlines := A_Index
Gui, +AlwaysOnTop +ToolWindow
Gui, Color, 0x202020, 0x333333
Gui, font, cFEFAF3
Gui, Add, Button, x5 y40 w60 gRunN, Run
Gui, Add, Button, x75 y40 w60 gRefreshH, Reload
Gui, Add, Button, x145 y40 w60 gNoteX, NoteX
Gui, Add, Button, x215 y40 w60 gDelL, Delete
Gui, Show, w%Guiw% h%Guih% x%GuiX% y%GuiY%
return
DelL:
Loop, %numlines%
{
	FileDelete, % NewF Inc ".txt"
	Inc++
}
Inc := 1
MsgBox,,,Deleted!,1
return
RunN:
UsedNumber := {}, WritedLines := 0
Loop, %numlines%
{
	Loop
		Random, randomNumber, 1, %numlines%
	Until, !UsedNumber[randomNumber]
	UsedNumber[randomNumber] := True
	; remembers which lines have already been used
	FileReadLine, TXTT, %MainF%, %randomNumber%	
	IF (99 = ++WritedLines) ;current file is filled
		++Inc, WritedLines := 1
	FileAppend, %TXTT%`n, % NewF Inc ".txt"
}
Reload
return
RefreshH:
Reload
return
NoteX:
Runwait, taskkill /im Notepad.exe /f
return
Gosub esc
esc::
GuiClose:
ExitApp

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Incremented text files with specified amount of lines

Post by ahk7 » 26 Sep 2022, 02:16

You can probably speed up and simplify the code in your RunN: label

* Read the entire source file into a variable
* Use the SORT command with the Random parameter, now your text is already random, so no longer need to keep track of which lines
* Loop parse entire variable
* Select a random number between 1-98
* FileAppend to file when random number is met

Post Reply

Return to “Ask for Help (v1)”