0

I am trying to use for loop in CMD to 1) obtain user input as a number 2) use that number to create "n" number of .txt files starting from 1.txt and ending at n.txt.

I have this so far:

@echo off
set /p n="Enter a number:"
for  %%n%% in (1, 1, %%n%%) echo %%n%% > %%n%%.txt

I played around with the "%" since it is in a batch file. If I just use this:

@echo off
set /p n="Enter a number:"
echo %n% was created

It will says "input" was created , if I add the "> %n%.txt" then it will create a file called n.txt with whatever the echo line says inside the .txt file and does not echo the result on the cmd screen.

Keep in mind that I am new to this, been playing with this particular challenge question for a few days and to no avail.

Any input is appreciated!

AMSAK
  • 13
  • 5
  • Open a command prompt, type `for /?` and look at the section for `for /L`. That should tell you everything you need to know. – SomethingDark Jan 29 '18 at 05:22

2 Answers2

0
set /p n="Enter a number:"
echo %n% was created
for  /l %%z in (1, 1, %n%) do echo %%z > %%z.txt

I guess typing randomly doesn't work so well.

ACatInLove
  • 175
  • 3
0

**Alternative use for for loop. Try this code. Simple use of batch command which will work for you.

set /p in="Enter a number:"
set /a n = 1
set /a count = %in% + 1

:loop
echo %n%
echo %n% > %n%.txt
set /a n = %n% + 1
IF %n% LSS %count% ( goto loop)
goto rest

:rest
SparRow
  • 62
  • 5
  • Thank you! This is excellent! However, we had to use for loop only for this particular case. – AMSAK Jan 30 '18 at 04:08