Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
1+2=46
- Posts: 25
- Joined: 23 Mar 2010 14:19
#1
Post
by 1+2=46 » 18 Apr 2010 10:19
Hi.
I am making a batch file where it's purpose will be understood whenever you see the code.
Ok, here's the code:
Code: Select all
@echo off
Cls
Title
Cls
Set RS=1000
:Loop
Color 0C
Ping localhost -n 1 -w 1000 >nul
Set /A R=%random%%%999+1
Set /A a=%R%
If %a% LSS 50 (
If %a% LSS 25 (
Goto Loop
)
If %R%==%RS% (
Goto Loop
)
Cls
Color 0A
Echo %R% is not greater than 50 and is not less than 25.
Echo -%R%->>%HomePath%\Desktop\Numbers.txt
Set RS=%R%
Ping localhost -n 3 -w 1000 >nul
Cls
Goto Loop
)
If %R% LSS 100 set sentence=%R% is greater than 50.
If %R% GTR 100 set sentence=%R% is greater than 50.
If %a% GTR 10 (
If %R% GTR 50 (
If %R% LSS 100 (
Set sentence=%R% is greater than 50.
)
If %R% GTR 100 (
Set sentence=%R% is greater than 50.
)
If %R% LSS 25 (
Set sentence=%R% is less than 25.
)
Echo %sentence%
Goto Loop
)
Goto Loop
For some reason the code doesn't works.
And by the way, the code is all made by me.
Last edited by
DosItHelp on 18 Apr 2010 21:52, edited 1 time in total.
Reason: Added code indentation to make the missing bracket near the end more obviose.
-
DosItHelp
- Expert
- Posts: 239
- Joined: 18 Feb 2006 19:54
#2
Post
by DosItHelp » 18 Apr 2010 21:48
The brackets are not balanced, a closing bracket missing somewhere near the end.
Another thing I noticed is that the "If %R%==%RS%" condition will never be true because R will will be 0<R<1000, but never 1000, because "%random% %% 999" will return a random number 0<=number<999.
DosItHelp?
-
1+2=46
- Posts: 25
- Joined: 23 Mar 2010 14:19
#3
Post
by 1+2=46 » 19 Apr 2010 06:51
DosItHelp wrote:The brackets are not balanced, a closing bracket missing somewhere near the end.
Another thing I noticed is that the "If %R%==%RS%" condition will never be true because R will will be 0<R<1000, but never 1000, because "%random% %% 999" will return a random number 0<=number<999.
DosItHelp?
No. If you pay attention you'll see that every time a number between 50 and 25 is picked up, a command is wrote like this: "Set RS=%R%". I made it like that, so the next number will never be the same as the past one.
And thank you. I just noticed the missing closing bracket. It was really the last one.
-
1+2=46
- Posts: 25
- Joined: 23 Mar 2010 14:19
#4
Post
by 1+2=46 » 19 Apr 2010 16:23
Okay, I modified the code so it works more perfectly.
But when I run the file I get a error.
Here it goes the new modified code:
Code: Select all
@echo off
Cls
Title
Cls
Set number=0
Set RS=1000
If not exist "%HomePath%\Desktop\Numbers.txt" (
Echo Numbers between 175 and 200:>>"%HomePath%\Desktop\Numbers.txt"
Echo.>>"%HomePath%\Desktop\Numbers.txt"
Ping localhost -n 1 -w 1000 >nul
)
:Loop
Color 0C
Ping localhost -n 1 -w 1000 >nul
Set /A R=%random%%%999+1
Set /A a=%R%
If %a% LSS 200 (
If %a% LSS 175 (
Goto Loop
)
If %R%==%RS% (
Goto Loop
)
Cls
Color 0A
If %b%==%0% (
Goto Loop
)
If %b%==%1% (
Goto Loop
)
If %b%==%2% (
Goto Loop
)
If %b%==%3% (
Goto Loop
)
If %b%==%4% (
Goto Loop
)
If %b%==%5% (
Goto Loop
)
If %b%==%6% (
Goto Loop
)
If %b%==%7% (
Goto Loop
)
If %b%==%8% (
Goto Loop
)
If %b%==%9% (
Goto Loop
)
If %b%==%10% (
Goto Loop
)
If %b%==%11% (
Goto Loop
)
If %b%==%12% (
Goto Loop
)
If %b%==%13% (
Goto Loop
)
If %b%==%14% (
Goto Loop
)
If %b%==%15% (
Goto Loop
)
If %b%==%16% (
Goto Loop
)
If %b%==%17% (
Goto Loop
)
If %b%==%18% (
Goto Loop
)
If %b%==%19% (
Goto Loop
)
If %b%==%20% (
Goto Loop
)
If %b%==%21% (
Goto Loop
)
If %b%==%22% (
Goto Loop
)
If %b%==%23% (
Goto Loop
)
If %b%==%24% (
Goto Loop
)
If %b%==%25% (
Goto Loop
)
Set number=%number% + 1
Set b=%number%
If %number%% GEQ 25 (
Goto Loop
)
Echo %R% is not greater than 200 and is not less than 175.
Echo -%R%->>%HomePath%\Desktop\Numbers.txt
Set RS=%R%
Ping localhost -n 3 -w 1000 >nul
Cls
Goto Loop
)
If %R% LSS 100 set sentence=%R% is less than 200.
If %R% GTR 100 set sentence=%R% is greater than 200.
If %a% GTR 10 (
If %R% GTR 200 (
If %R% LSS 200 (
Set sentence=%R% is greater than 200.
)
If %R% GTR 100 (
Set sentence=%R% is greater than 200.
)
If %R% LSS 175 (
Set sentence=%R% is less than 175.
)
Echo %sentence%
Goto Loop
)
)
Goto Loop
Could someone help me, please?
-
jeb
- Expert
- Posts: 1055
- Joined: 30 Aug 2007 08:05
- Location: Germany, Bochum
#6
Post
by jeb » 22 Apr 2010 00:03
Hi 1+2=46,
will always fail, because it expands to
Because at first %b% is empty, %0 expands to the name of your batch file and the second % will be ignored.
I suppose that is not exactly what you want
You should use some echo's for debugging, than you can find this types of bugs
jeb
-
1+2=46
- Posts: 25
- Joined: 23 Mar 2010 14:19
#7
Post
by 1+2=46 » 22 Apr 2010 07:20
jeb wrote:Hi 1+2=46,
will always fail, because it expands to
Because at first %b% is empty, %0 expands to the name of your batch file and the second % will be ignored.
I suppose that is not exactly what you want
You should use some echo's for debugging, than you can find this types of bugs
jeb
I tried:
(at the start of the code)
And still doesn't works.
-
jeb
- Expert
- Posts: 1055
- Joined: 30 Aug 2007 08:05
- Location: Germany, Bochum
#8
Post
by jeb » 22 Apr 2010 10:51
Hi,
as I wrote, your code will always fail
Code: Select all
If %b%==%0% (
Goto Loop
)
If %b%==%1% (
Goto Loop
)
If %b%==%2% (
Goto Loop
)
You should try to debug it with some echo lines, like
echo %b% == %0%
to see why it will fail
jeb
-
avery_larry
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
#9
Post
by avery_larry » 03 May 2010 09:01
Can't use 0 - 9 as the variables because they expand as the command line arguments:
%0% will expand to just %0
%1% will expand to just %1
...
If you MUST use the numbers as the variables, you will have to use delayedexpansion and !0! !1! etc. Haven't found any other way to do it.