1.

Solve : Total beginner question! How do you create a batch file to count to 100??

Answer»
When I say total beginner.. i really mean it!
Have been GIVEN a task to learn dos in half a day! Struggling big time!
One of the missions is to create a batch file that counts to 100.
I'm failing miserably at this!!

HELP Type in FOR /?
that will get you startedermm.. not really.
much more of a totally beginner than I imagined.. Although I have only been doing this since 9 today!
You can put this in a .bat file. It pauses at the end for the sake of being able to see it has done what you want... It's really fast though so im not sure if this helps you 'complete the mission'?

@ECHO OFF
FOR /L %%i IN (1,1,100) DO (
ECHO %%i
)
PAUSE
set count=0
:loop
set /a count=%count%+1
if not #%count%#==#100# goto loop
goto endQuote from: sfin on September 27, 2013, 03:32:01 AM
learn dos in half a day!

This is not possible. Who gave you this "task"?
Quote from: SALMON Trout on September 27, 2013, 10:27:50 AM
This is not possible. Who gave you this "task"?

Quote from: patio on September 27, 2013, 10:29:28 AM


Ahh, it's *your* fault! So in detail, there are three ways to do what you want to do.

The first is the most useful in scripts, which is a GOTO based loop, which would go something like this:

Code: [Select]set counter=0
:loop
set /a counter=counter+1
echo %counter%
if not %counter% == 100 goto :loop
The first line, gives us a base value to work with, and also truncates (empties) any previous value the variable may have had.

The second line, :loop, is a marker, (A batch label) to return to later
The third line is the key line of the program, it increases the variable 'counter' by one, it works using the arithmetic argument '/a', which tells set to deal with the phrase after the equals like a calculator would.
The fourth is what proves that the script is counting, it prints the current number to the screen, no fuss about it.
The fifth is what makes the program stop when we want it to, as well as making it continue until we want it to, it literally translates to "start again from the line with 'loop' written on it, unless you are already up to 100"

The second way of doing a recursive loop like this, is the SIMPLEST way of achieving what you want, and will do it all in one line.
for /L %%n in (1,1,100) do (echo %%n)
This uses a for loop, which is broken up into three parts, FOR, IN, and DO
FOR
tells us what argument (letter variable) to deal with, in this case 'n', prefixed with a double %
IN
tells us how and where to find our argument, we have put a '/L' after the FOR part, so we are dealing with numbers (as opposed to files,) in the brackets we have 1,1,100; start, step, and end, so we start at 1, step forward 1 at a time, and end at 100. after each step it passes the number through %%n into
DO
is a function to deal with, it is just ECHO %%n, so it prints the number it is up to to the screen,


The final method is almost identical to the first, except we use CALL instead of GOTO

Code: [Select]set counter=0
:loop
set /a counter=counter+1
echo %counter%
if not %counter% == 100 call :loop
This is different in the way that it deals with the looping process. I can explain FOR and CALL in more detail if you want, but if you don't understand want I just wrote, then read it again more thoroughly, or go for a slower step by step tutorial somewhere. I hope this helpedI noticed no one posted this, but you can use
Code: [Select]set /a LoopToOneHundred.Counter+=1
to count %LoopToOneHundred.Counter% up by 1, instead of writing out
Code: [Select]set /a LoopToOneHundred.Counter=LoopToOneHundred.Counter+1
(which can get long when your variable gets over 10 letters).Quote from: Lemonilla on September 29, 2013, 10:49:11 AM
you can use
Code: [Select]set /a LoopToOneHundred.Counter+=1
to count %LoopToOneHundred.Counter% up by 1

Yeah Thanks Lemonilla, I forgot to add that in the notes, but I was THINKING about it,

Also you could say
Code: [Select]if /I %counter% LSS 100 goto :loop
which gives the script a bit more integrityQuote from: spivee on September 29, 2013, 06:35:25 PM
Yeah Thanks Lemonilla, I forgot to add that in the notes, but I was thinking about it,

Also you could say
Code: [Select]if /I %counter% LSS 100 goto :loop
which gives the script a bit more integrity

What does the /I switch do here? (I know what it means, I just wondered why you use it in that line)


Quote
What does the /I switch do here?

I wasn't aware that it wasn't necessary to include the /I switch, until after I posted that. I learned almost all of what I know from the help utility, and it is modeled around using == and switching to explicits with /I if necessary, which I assume is because the /I switch once upon a time wasn't optional.Quote from: spivee on September 30, 2013, 02:31:19 PM
switching to explicits with /I

I am not sure what you mean by "switching to explicits". All the /I switch does is make string comparisons case-insensitive e.g. if /I "ABC"=="abc". Since numbers don't have case, it is unnecessary with if %counter% LSS 100.


Quote from: Salmon Trout on September 30, 2013, 03:46:49 PM
I am not sure what you mean by "switching to explicits". All the /I switch does is make string comparisons case-insensitive

Oh, you are right, I misread the help util two years ago then... thanks for clearing that up then


Discussion

No Comment Found