Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Concatenate two BAT commands to single line output  (Read 8741 times)

0 Members and 1 Guest are viewing this topic.

joewolfe

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 10
    Concatenate two BAT commands to single line output
    « on: December 06, 2020, 02:31:04 AM »
    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

    pause

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Concatenate two BAT commands to single line output
    « Reply #1 on: December 06, 2020, 11:56:28 AM »
    In 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  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    joewolfe

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Windows 10
      Re: Concatenate two BAT commands to single line output
      « Reply #2 on: December 07, 2020, 07:39:15 PM »
      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.

      joewolfe

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Windows 10
        Re: Concatenate two BAT commands to single line output
        « Reply #3 on: December 07, 2020, 08:44:53 PM »
        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!