Page 1 of 1

How to add multiple numbers found in a text document

Posted: 11 Sep 2011 03:40
by Rileyh
Hi,
I am writing a script that is supposed to find the multiples of 3 and 5 below 1000 and write them in a text document labelled multiples.txt. It then is supposed to add those numbers. I cannot get it to add the numbers it writes. I can get it to write the numbers in the first place.

Here is my code:
@echo off
echo.>,multiples.log
for /l %%a in (0,3,1000) do (
echo %%a>>multiples.log
)
pause

for /l %%a in (0,5,1000) do (
echo %%a>>multiples.log
)
pause

Feel free to change my code or completely write a new one.

Thanks,
Rileyh

Re: How to add multiple numbers found in a text document

Posted: 11 Sep 2011 06:11
by !k

Re: How to add multiple numbers found in a text document

Posted: 11 Sep 2011 06:18
by aGerman
Rileyh wrote:multiples of 3 and 5 below 1000

0 isn't a multiple of 3 or 5 also 1000 is not below 1000.
For that reason: start is 3 (or 5), step is 3 (or 5), end is 999.

Code: Select all

@echo off
>multiples.log type nul

for /l %%a in (3,3,999) do (
  >>multiples.log echo %%a
)
pause

for /l %%a in (5,5,999) do (
  >>multiples.log echo %%a
)
pause

set /a n=0
for /f %%i in (multiples.log) do set /a n+=%%i
echo %n%
pause



Without additional log file:

Code: Select all

@echo off
set /a n=0
for /l %%a in (3,3,999) do set /a n+=%%a
for /l %%a in (5,5,999) do set /a n+=%%a
echo %n%
pause

Regards
aGerman

Re: How to add multiple numbers found in a text document

Posted: 11 Sep 2011 06:49
by Rileyh
Thanks aGerman
It works perfectly

Regards,
Rileyh

Re: How to add multiple numbers found in a text document

Posted: 11 Sep 2011 19:24
by Rileyh
@aGerman
The program has added incorrectly
I have made some modifications to the script and it seems to not work.
Here is my code:
@echo off
set /a n=0
for /l %%a in (0,3,1000) do set /a n+=%%a
for /l %%b in (0,5,1000) do set /a n+=%%b
set /a d=3
set /a e=5
for /l %%c in (1,1,1) do set /a f=%%a - %d%
for /l %%g in (1,1,1) do set /a h=%%b - %e%
for /l %%i in (1,1,1) do set /a i=%%f + %%h
echo %i%
pause

Could you "repair" the code?

Regards,
Rileyh

PS- Could you tell me a website for a good tutorial- it would save me having to keep posting on a forum and bothering people!
Dont go to too much trouble.

Re: How to add multiple numbers found in a text document

Posted: 12 Sep 2011 05:20
by trebor68
You will have any multiples from a value in one row.
In one row: 0 3 6 9 12 15 18 ...
In another row: 0 5 10 15 20 ...

Code: Select all

@echo off
echo.>multiples.log
for /l %%a in (0,3,1000) do echo.|set/p.=%%a >>multiples.log
echo.>>multiples.log
for /l %%a in (0,5,1000) do echo.|set/p.=%%a >>multiples.log
echo.>>multiples.log
pause


Another way is any value row by row.

Code: Select all

@echo off
echo.>multiples_3.txt
for /l %%a in (0,3,1000) do echo %%a>>multiples_3.txt
echo.>multiples_5.txt
for /l %%a in (0,5,1000) do echo %%a>>multiples_5.txt
findstr /x /g:multiples_3.txt multiples_5.txt>multiples.txt
pause


Must first read the batch file the text file line by line. Then identify within the row the numbers. Then check to see if they are the right numbers. And then add this.

If you want to count the value?
It is possible that you must have add the setlocal command.
Here the code:

Code: Select all

@echo off
echo Counting from 0 to 1000
echo.
set val1=3
set val2=5
set an=0
set bn=0
set cn=0
for /l %%a in (0,%val1%,1000) do set /a an+=1
for /l %%a in (0,%val2%,1000) do set /a bn+=1
for /l %%a in (0,%val1%,1000) do (set /a val3=%%a-%%a/val2*val2) & if !val3!==0 set /a cn+=1
echo multiples from %val1% are: %an%
echo multiples from %val2% are: %bn%
set /a i=%an% + %bn%
echo %i%
echo It is also possible that some numbers double counted.
echo.
echo multiples from %val1% and %val2% are: %cn%
pause


EDIT

To count the value for the results in code number one:

Code: Select all

@echo off
set row=0
for /f "tokens=*" %%a in (multiples.txt) do (
  set /a row+=1
  set num=0
  for %%A in (%%a) do set /a num+=1
  echo row !row!: !num!
)


To count the value for the results in code number two:

Code: Select all

@echo off
set val1=0
set val2=0
set val3=0
for /f %%a in (multiples_3.txt) do set /a val1+=1
for /f %%a in (multiples_5.txt) do set /a val2+=1
for /f %%a in (multiples.txt) do set /a val3+=1
echo %val1%  ---  %val2%  ---  %val3%