Batch File: Assign random line of text file as variable for later use -


i'm trying write simple batch file personal use...it's complete except 1 thing i'm stumped on. easy fix (i'm illiterate when comes code).

basically i'm trying have script choose random line text file, couple times couple different text files, wish assign output each text file variable can use them in various combinations...then repeat process. here have right now...

@echo off :start setlocal setlocal enabledelayedexpansion enableextensions  set "list1=list1.txt" /f %%a in ('type "%list1%"^|find /c /v ""') set /a numlines=%%a set /a list1random=(%random% %% %numlines%) if "%list1random%"=="0" (set "list1random=") else (set "list1random=skip=%list1random%") /f "usebackq tokens=* %list1random% delims=" %%a in (`type %list1%`) (     >> output.txt echo %%a ) :finish endlocal goto start` 

this procures random line, , spits text file. well, next step, take random result , assign variable...

@echo off :start setlocal setlocal enabledelayedexpansion enableextensions  set "list1=list1.txt" /f %%a in ('type "%list1%"^|find /c /v ""') set /a numlines=%%a set /a list1random=(%random% %% %numlines%) if "%list1random%"=="0" (set "list1random=") else (set "list1random=skip=%list1random%") /f "usebackq tokens=* %list1random% delims=" %%a in (`type %list1%`) (     set output1=%%a ) >> output.txt echo %output1% :finish endlocal goto start 

now output ceases random...instead last line of referenced text file.

edit: site suggested question similar mine. however, person having trouble getting script choose valid line. valid line every time, , random 1 (when check via echo), non-random line when proceeding on, assigning output variable. don't understand because seems post-facto derandomization. i.e. difference between 2 scripts has nothing procuring random result, result after has it, right?

i appreciate in advance, last step before know need finish this, i'm excited!

sorry, you're right...anyways, figured out simple workaround, not quickest in terms of processing time, whatever. allow initial part of script spit out random result text file (as seemed work fine) reference text file variable.

@echo off :start set "list1=list1.txt" /f %%a in ('type "%list1%"^|find /c /v ""') set /a numlines=%%a set /a listchoice=(%random% %% %numlines%) if "%listchoice%"=="0" (set "listchoice=") else (set "listchoice=skip=%listchoice%") /f "usebackq tokens=* %listchoice% delims=" %%a in (`type %list1%`) ( >> listoutput.txt echo %%a ) set /p list=<listoutput.txt >> result.txt echo %list% :finish del listoutput.txt goto start 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -