Computer Hope

Microsoft => Microsoft DOS => Topic started by: DarrenReeder on October 26, 2009, 11:18:54 AM

Title: How to cycle through numbers..?
Post by: DarrenReeder on October 26, 2009, 11:18:54 AM
I am currently making a small batch file game and im making it so your account (i already made a register system) has money... basicly i plan to make it so you have a Directory in yuor account files called 'Money' then inside there it will have another folder with your money (ie: Money\1000)... ..

now if i want to add 500 to my 1000 money i want to be able to...Rmdir 1000 and mkdir 1500 and i plan to do this using Variables.... now basicly i want to know if i can cycle through number 0-999999 so i can do :

Code: [Select]
number=0-999999
if exist %number% goto A

:A

number2=number+500
rmdir number
mkdir number2

ok, so thats sort of what i wanna do? the 'number' variable i basicly want it like %random% but it goes through all the numbers instead of chooses one... i hope this sorta makes sense and it wuld be great for help on how to make the 'if exist' cycle through loads of numbers :-)
Title: Re: How to cycle through numbers..?
Post by: gpl on October 26, 2009, 11:42:04 AM
I think this is too clunky, why not have a FILE called Money that holds your value ? It is easier to read and write a file of a known name than it is to check if a directory exists.

Unless you have a really good reason for using a directory, isnt this simpler ?
Code: [Select]
:: Read the money value
Set /P MyMoney=<Money

:: add 500 to it
Set /A MyMoney=MyMoney + 500

:: save the value back
Echo MyMoney>Money
Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 26, 2009, 11:48:26 AM
i use a register system so you log in everytime u load the batch file...so i need it to be a permanent Save...if you know what i mean?
Title: Re: How to cycle through numbers..?
Post by: gpl on October 26, 2009, 11:55:40 AM
The file Money is permanent, across logins - you only load a value as you need to use it -- think config files
Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 26, 2009, 11:58:20 AM
im not too sure i understand your thing..could you explain like what its doing please? lol.. im not too confident with this stuff..
Title: Re: How to cycle through numbers..?
Post by: Helpmeh on October 26, 2009, 03:42:21 PM
Gpl you are messing up the op and not even touching on what the op wants. Darren, try looking at FOR /?

Your code should be (I think):

for /l %%a in (0,1,99999) do if exist %%a set mon=%%a &goto continue
:continue
commands
Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 26, 2009, 07:19:24 PM
so that would mean i could then do

if exist %%a...



and %%a would hold all numbers from 0-999999 ?
Title: Re: How to cycle through numbers..?
Post by: billrich on October 26, 2009, 08:38:14 PM

C:\batextra>type mon.bat
Code: [Select]
@echo off

FOR /L %%i IN (0,1,999999) DO (
echo %%i
if %%i==89 set var=%%i && goto eof

)
:eof
echo outside var=%var%

OUTPUT:

C:\batextra>mon.bat
.
.
.

76
77
78
79
80
81
82
83
84
85
86
87
88
89
outside var=89

C:\batextra>echo %var%
89

C:\batextra>

REM:  The For loop variable, %%i,  only contains one number at any one time. At the end of the loop %%i = %var% = 999999

After the loop works as you need it to,  then add your other commands.
The numbers inside the for loop will always exist.  The test for money folders will require more than a test of the for loop counter.
Title: Re: How to cycle through numbers..?
Post by: gh0std0g74 on October 26, 2009, 09:52:42 PM
I am currently making a small batch file game
a small question just to satisfy my curiosity. why do you want to use batch for programming a game, when for one thing, it can't even do floating maths properly and moreover, you are dealing with money. My suggestion, if you want to do games or solve other real problems...use a programming language such as Python (or others), but not batch.

FYI, you can build games easily with pygame. (http://www.pygame.org/news.html)
Title: Re: How to cycle through numbers..?
Post by: BatchFileBasics on October 26, 2009, 09:57:45 PM
although i agree with you ghost, he probably isn't trying to make a legit programming game, im sure hes just trying to make something he can say "yup, i made it" then stop playing it maybe 1/2 hour tops later and goes off to start a new one.
Title: Re: How to cycle through numbers..?
Post by: Salmon Trout on October 27, 2009, 02:52:42 AM
Good luck with 999999 files or folders in a directory...
Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 27, 2009, 06:18:30 AM
i dont want 99999 folders... i want it so like....

the folder is my money... so i got the directory..

money\500 (500 is my cash)

then i wanna find a way of Getting that 500 into my batch file so i can do

500+1000(example)


So mabe im trying to do the wrong thing?
Title: Re: How to cycle through numbers..?
Post by: BC_Programmer on October 27, 2009, 07:04:40 AM
well think about it- why use a Folder?

Why not use a file, say money.txt. Then you load the money amount with:

Code: [Select]
type money.txt | set /p %money%=

and retrieving the money amount would be:

Code: [Select]
echo %money%>money.txt
Title: Re: How to cycle through numbers..?
Post by: Salmon Trout on October 27, 2009, 07:26:30 AM
When I were a lad (Northern English dialect) we used QBasic for these things.
Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 27, 2009, 07:55:21 AM
I understand why people say not to use Batch file for a game lke this...but im just making it on batch file because i wanna see what is possible using Batch files.....

--------


BC_Programmer, So it is possible to read from a txt file?  i didnt know you could read from them to be honest... is that what type does?
Title: Re: How to cycle through numbers..?
Post by: BC_Programmer on October 27, 2009, 08:11:38 AM
type shows it on the screen. What I did there was use set /p. Set /p let's you display a prompt and get's the user to type in a value, and the value is stored in the specified variable.

however, in this case, I used the  type command to get the contents of the file, and then redirected that to the set /p command- in essence, it "automates" the input which then becomes the contents of the file.

I mislabelled the second one there, rather then retrieving it should be saving, that's what the echo does.
Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 27, 2009, 08:12:58 AM
so,... i put it in the code like this?

type money.txt | set /p %money%=

then whatever i type in it will overwrite it?
Title: Re: How to cycle through numbers..?
Post by: BC_Programmer on October 27, 2009, 08:14:52 AM
if money.txt had the value 500 in it, then that would set the %money% variable to 500.

Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 27, 2009, 08:17:55 AM
but i thought that when you do

set /p %money%=

whatever u type into that, it makes the %money% variable that....

---

basicly i need to change whatever is in that txt so its like...%money%+500
Title: Re: How to cycle through numbers..?
Post by: gpl on October 27, 2009, 08:43:15 AM
but i thought that when you do

set /p %money%=

whatever u type into that, it makes the %money% variable that....

---

basicly i need to change whatever is in that txt so its like...%money%+500
Yes, that is true, but if you pipe or redirect a file into the Set /P statement, the first line of the file is read in, rather than the keyboard.
Title: Re: How to cycle through numbers..?
Post by: DarrenReeder on October 27, 2009, 08:47:14 AM
this is quite confusing and DOS is suupose to be the easier stuff  :-\


----

Can som1 show me code so that if i got a money.txt file with '500' in it, how i change that 500 to 600? then walk me through what it is doing  -I know this is sorta like a 'give me the code' but i wuld like to learn what im doing

thanks for all this help everyone so far
Title: Re: How to cycle through numbers..?
Post by: BC_Programmer on October 27, 2009, 10:27:12 AM
Code: [Select]
type money.txt | set /p %money%=

Code: [Select]
echo %money%>money.txt

using the above:

Code: [Select]
if not exist money.txt echo 100>money.txt
type money.txt | set /p %money%=
set /a money=%money%+100
echo %money% > money.txt
Title: Re: How to cycle through numbers..?
Post by: billrich on October 27, 2009, 12:59:11 PM

Can somone show me code so that if I have a money.txt file with '500' in it, how I change that 500 to 600? Then walk me through what it is doing  -I know this is sorta like a 'give me the code' but I would like to learn what im doing.

Thanks for all this help everyone so far.

C:\>type  mon.bat
Code: [Select]
@echo off

if not exist money.txt echo 500 > money.txt

set /p money=< money.txt

REM  The redirect above works better than a pipe "|"
REM type money.txt | set /p %money%=
REM type money.txt | set /p money=


echo money =%money%

set /a money=%money%+100

echo %money% > money2.txt

echo check what is in money2.txt
type money2.txt
del money.txt

Echo Run again


OUTPUT:  Run mon.bat

C:\>mon.bat
money =500
Press any key to continue . . .
check what is in money2.txt
600
Run again.
C:\>
Title: Re: How to cycle through numbers..?
Post by: billrich on October 27, 2009, 02:03:35 PM
Redirection and Pipes

Normally, input is taken from the keyboard and output goes to the console.

 Redirection allows input and output to refer to a file or device instead.

 Pipes allow the output of one program to be used as input to another program.

These symbols only work with programs that read from "standard input" and write to "standard output" but fortunately this includes most DOS commands.

The < symbol causes file to be fed to the program as input.
The > symbol causes the program's output to be sent to the following file or device.
The >> symbol causes the program's output to be appended to the file or device.

The | symbol (the pipe) causes the output of the preceding program  to be sent to the following program

Title: Re: How to cycle through numbers..?
Post by: Helpmeh on October 27, 2009, 03:39:52 PM
To get the first line of a file into a variable, do this:

set /p variable=<Money.txt
echo The first line of Money.txt is %variable%.
Title: Re: How to cycle through numbers..?
Post by: billrich on October 27, 2009, 03:52:09 PM
To get the first line of a file into a variable, do this:

set /p variable=<Money.txt
echo The first line of Money.txt is %variable%.

Thanks,  Helpmeh agrees with the code in Post # 22 above.
Title: Re: How to cycle through numbers..?
Post by: Helpmeh on October 27, 2009, 03:55:36 PM
Thanks,  Helpmeh agrees with the code in Post # 22 above.
My iPod stopped loading the page at reply 16, but whatever. 
Title: Re: How to cycle through numbers..?
Post by: BC_Programmer on October 27, 2009, 04:06:21 PM
they both work, the one using < is shorter though.
Title: Re: How to cycle through numbers..?
Post by: Helpmeh on October 27, 2009, 04:13:37 PM
they both work, the one using < is shorter though.
I have never actually used the pipe way...pretty cool to know.
Title: Re: How to cycle through numbers..?
Post by: billrich on October 27, 2009, 04:24:43 PM
I have never actually used the pipe way...pretty cool to know.


I could not get the pipe to work correctly.  The wrong value was assigned

to the variable.

Neither of the following worked for me :

type money.txt | set /p %money%=

type money.txt | set /p money=
Title: Re: How to cycle through numbers..?
Post by: Helpmeh on October 27, 2009, 04:36:17 PM
If %money% isn't assigned, then wouldn't set %money%=whatever bring an error because there was no variable name?
Title: Re: How to cycle through numbers..?
Post by: Salmon Trout on October 27, 2009, 04:41:02 PM
I thought that in NT, the receiving end of an anonymous pipe is launched in a separate child process, so any environment changes will not survive, so, BC_Programmer, how did you get the pipe method to work?



Title: Re: How to cycle through numbers..?
Post by: billrich on October 27, 2009, 06:35:09 PM
If %money% isn't assigned, then wouldn't set %money%=whatever bring an error because there was no variable name?

Yes, I agree just the variable and not the contents of variable.  I  got no error messages, just the wrong answer.   I did not use local variables, so the value was many times  from the previous run. Finally I set the variable to zero at the beginning of the run and there after only zero appeared. No value was ever assigned to the variable with the pipe.

Strange:  it looks like it should work.

The pipe in CMD, the command interpreter,   does not work like UNIX and C.

Title: Re: How to cycle through numbers..?
Post by: gh0std0g74 on October 27, 2009, 06:40:45 PM
DOS is suupose to be the easier stuff  :-\
no, its not. there are programming limitations in DOS (cmd.exe)  that makes it clumsy(if not impossible) to do certain things. eg floating point maths, arrays, ability for error control, date manipulation etc. If you want to learn programming, start with a real programming language
Title: Re: How to cycle through numbers..?
Post by: BC_Programmer on October 27, 2009, 08:59:42 PM
I thought that in NT, the receiving end of an anonymous pipe is launched in a separate child process, so any environment changes will not survive, so, BC_Programmer, how did you get the pipe method to work?





Hmm, Not sure. Not sure how I had it working either, maybe it just appeared to work; I was messing about with the commandline seeing how I could force the contents of the file to be the stdin of set /p and I figured I do it with more, may as well give piping a shot. No idea how it worked, must have been a fluke and I had set a variable to the same value I was expecting (???). I believe it's true though, that as far as the | is involved each program is given a inherited environment. (it was my understanding that they could inherit a handle to the same environment, and thus change the values within it... but I suppose cmd doesn't do that.

redirection works, and it's shorter, too.

What I did probably stems from when I used "ANSWER.com" to perform input queries in Pure DOS; since it was a program using the pipe to force input wasn't really that esoteric. (And of course the fact that the environment block was relatively unchanged for the execution of any number of programs was a big plus).