1.

Solve : Concatenate two BAT commands to single line output?

Answer»

Hello! 

Thanks in advance for any help you can provide, I have what I THINK should be a simple issue but I cannot seem to find the solution.  I have a very small batch file that i use to select two random WORDS from a text file of thousands of words.  Currently, it returns results in two separate rows, such as:

RandomWordOne
RandomWordTwo

I would instead like it to return these on one line with a space in between, like this:

RandomWordOne RandomWordTwo

Below is the simple code I am using, I have connected the commands on one ROW with & but cannot get the output on one line:

echo off

set /a rnd=%random%%%370103
set /a rnd2=%random%%%370103
for /f "tokens=1,2" %%a in (list.txt) do if %rnd%==%%a echo %%b & for /f "tokens=1,2" %%a in (list.txt) do if %rnd2%==%%a echo %%b

pauseIn all honesty I couldn't really determine what your code was doing so I invented my own that may give you some IDEAS. I used a list of 1000 words (one per line) and used the batch file below to randomly choose two of them.

Code: [Select]echo off
setlocal enabledelayedexpansion

set x=0
for /f %%i in (list.txt) do (
  call set word.%%x%%=%%i
  call set /a x+=1
)

set min=1
set max=1000

set /a rnd=%random% %% (%max% - %min% + 1)  + %min%
set /a rnd2=%random% %% (%max% - %min% + 1)  + %min%

call echo !word.%rnd%! !word.%rnd2%!

If you do run the code, be aware of the lag time while the code loads an array of the words.

Good luck  Thank you for the reply!  I tried this but there was what I would consider an extreme lag in processing this way, the list of words and PHRASES I'm using is ~370k entries long and the intention of this file is to produce multi-word sentences at a rate of around 5 seconds each, give or take.After smashing my head into my desk a few times I found the solution, I just added a "set XX=%%b" to each "do if" statement, then echoed each XX on one line at the end.  Thanks everyone for reading and thanks for the response!



Discussion

No Comment Found