0
echo off
:abc
Echo ^ text ^ >> C:/files/text_%random%.txt
goto abc

How do I do the goto only 15 times?

3 Answers 3

3

Instead of using goto multiple times, you should use a for loop.

Here, you can specify that the loop should run 15 times.

echo off 
SETLOCAL ENABLEDELAYEDEXPANSION
for /L %%i in (1,1,15) do (Echo ^ text ^ >> C:/files/text_!random!.txt)

The variable %%i will be equivalent to 1, 2, …, but it will not be used inside the loop.

6
  • Akina, it would be good if your answer was not just a piece of code, but if you explained what you did in order to get rid of goto statements, and why your answer is better.
    – slhck
    Aug 3, 2018 at 8:17
  • @slhck I solve the task rather than edit author's solution. This is not a learning task, using goto is not a dogma. I think so...
    – Akina
    Aug 3, 2018 at 9:07
  • 2
    What I meant is, on Stack Exchange, we prefer answers that give a little explanation rather than just posting a piece of code. If someone else happens to find your answer, they might learn something from it (i.e. why is a for loop better in this case?).
    – slhck
    Aug 3, 2018 at 9:10
  • @slhck English is not my native language so it's difficult for me to explain such things for novices. When I try to do it I obtain the explaination with a 'specialist level' - but those specialist have no problems to understand.
    – Akina
    Aug 3, 2018 at 9:19
  • Hope this will be of help/ref of some sort.. ( :
    – p._phidot_
    Aug 3, 2018 at 10:14
0

How do I do the goto only 15 times?

FOR /L %%v in (start, step, end) do *something* will do what you need, but if you like using GOTO for this task, you can first set a variable equal to a start value, then increment (or decrement) it each time the repeated operation is performed, testing each time if it has reached the end value. Note that you can start an echo off line with the @ symbol to prevent it being echoed to the console.

@echo off
set numval=0
REM you can use a variable or else a magic number in the loop
set endval=15
:abc
Echo ^ text ^ >> C:/files/text_%random%.txt
set /a numval +=1
if %numval% lss %endval% goto abc
REM or if %numval% lss 15 goto abc
echo Done 15 times
0

In one line in command line:

@cmd /v /c "for /L %l in (1 1 15)do @Echo ^ text ^ >>"C:/files/text_!random!.txt""

You must log in to answer this question.