-1

So I wanted my cmd to select random sentences.

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

Im using this but inside the list.txt file I have sentences with spaces. For some reason cmd only shows the first word and it doesnt with the rest.

For example a sentence in the file.

Hello how are you?

This would show as

Hello and that's all.

But if I type it like this

Hello_how_are_you?

then it does show all the words but it does include all the _'s

Any idea how to make the spaces show without replacing them with signs?

1
  • If you use "tokens=1*" then a will hold the first field, and b will hold the rest of the line. Commented Jun 26, 2016 at 11:44

1 Answer 1

0

You can instruct the for loop to grab all remaining tokens using the wildcard token '*' like this:

set /a rnd=%random% %% 5 
for /f "tokens=1,*" %%a in (list.txt) do if %rnd%==%%a echo %%b

Here, the first token is assigned to %%a and all of the remaining tokens to %%b. Note that any whitespace between tokens is replaced by exactly one blank.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.