Jump to content
Macro Express Forums

Can you select a random line from a txt document

Recommended Posts



I have ME - not the pro-version. just the normal one


I have a list of 200 lines in a text file 


I want the macro to select one of these lines at random each times it's ran and store the selected lines text in T1


How can I do this?



Link to comment
Share on other sites

It's been a while since I worked in the old MEP, so I might get some things wrong.
I would first use Text File Process and count the number of lines and store in an integer variable. Then create an integer variable using Set Random Variable, or whatever it's called in ME. Then use text file process command and use the "Start at" with your random number and set the "Number of Records" to 1.

Link to comment
Share on other sites

Cory's solution sounds very workable.


If you know the exact number of lines the text file contains, something like this might work well enough. But Cory's solution is better.


Variable Set Integer %RandomValue% to a random value between 1 and 200
Variable Modify Integer: %RandomValue% = %RandomValue% - 1
Text Type (Simulate Keystrokes): <CONTROL><HOME> // Go to top of text file
Repeat with Variable: Repeat %RandomValue% times // Go down %RandomValue% lines
  Text Type (Simulate Keystrokes): <ARROW DOWN>
End Repeat
Text Type (Simulate Keystrokes): <HOME><SHIFT><END> // Select the entire line
Clipboard Copy
Variable Set String %T1% from the clipboard contents
Text Type (Simulate Keystrokes): <HOME> // Deselect to start of line



Link to comment
Share on other sites

There's another trick one can do to count. Read the entire file in and get the number of characters. Then replace the carriage return or line feed with nothing. Subtract the second length, and that's the number of lines. 

Link to comment
Share on other sites

In the list of commands at the left in the editing pane I noticed a    Variable set Decimal - Set to a Random Value

Also there, I see 'Repeat' functions, including 'Repeat until', which could be used to increase a variable to the random value.

It seems likely therefore you could set a random variable, and do a Repeat Until loop until that variable is exhausted.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...