# Counting Batch File



## Newrad0603 (Jul 23, 2007)

I am an intern at a computer company. We have these test scripts in batch that test our products. I need help putting a counter in the test scripts so that every time you run the test file, the counter adds one if the test succeeds, then displays the total number of successes.

If anyone can help, it would be greatly appreciated. I only know enough batch to make our test scripts, so this is proving rather difficult.

Thanks


----------



## dez_666 (May 30, 2007)

you should be able to do this


```
set /a Succeeds=<C:\succeeds.txt
```
which will take whatever number is in succeeds.txt and set it to the # of succeeds

then, when the condition for a successful test goes off, just add 1


```
set /a Succeeds+=1
```
which adds 1 to the succeed count..

then output it to the file


```
@echo %Succeeds%>C:\succeeds.txt
```
which is a crude way to write the new number of succeeds to the same file xD

post on your results!

oh and welcome to TSG ^___^


----------



## Newrad0603 (Jul 23, 2007)

Ok, this makes sense, but it doesn't work. If I put 0 in succeeds.txt to initiate it, the end result is that succeeds.txt contains "ECHO is off." I don't know if this has to do with the fact that echo is already being used or not, but I need a little more help.

Thanks

And here is the original test script, hope it helps:

cls

@echo off
EXEC.EXE TEST.CFG INTERFACE=USBMULTILINK PORT=USB1
if errorlevel 1 goto bad
goto good

:bad
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
goto done

:good
echo Success! Success! Success! Success!
:done


----------



## dez_666 (May 30, 2007)

Okay maybe it would be better to use the SET command to find the value of succeeds and redirect that rather than echoing it.

so like

set succeeds>C:\succeeds.txt

okay so lets implement it..


```
cls

@echo off
set succeeds=<C:\succeeds.txt
EXEC.EXE TEST.CFG INTERFACE=USBMULTILINK PORT=USB1
if errorlevel 1 goto bad
goto good

:bad
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
echo Error*********Error*********Error
goto done

:good
echo Success! Success! Success! Success!
set /a succeeds+=1
set succeeds>C:\succeeds.txt
```
does that look ok?

i didnt even bother looking up the set command this time, all that came out of my head lol


----------



## Newrad0603 (Jul 23, 2007)

It works, but it says "Missing operand", I don't know if that is significant or not.


----------



## Newrad0603 (Jul 23, 2007)

Fixed it, thanks again.


----------



## dez_666 (May 30, 2007)

Can you try this for me? remove the @echo off

then run the batch script from the command prompt, and look through it, so then you know which command is giving that output. that way we can see about it running smoother. 

but the rest of it works correctly? as in it is able to find the number of succeeds and add to it correctly?


----------



## Newrad0603 (Jul 23, 2007)

set /a succeeds=<C:\succeeds.txt

this line produces the missing operand

but the counting works correctly, so it doesn't make sense to me


----------



## dez_666 (May 30, 2007)

OH haha i know why.

the "/a" switch makes the set command think its doing arithmetic, like the set /a succeeds+=1 adds 1 to the current number of succeeds. so remove /a and it should be fine.


----------



## Newrad0603 (Jul 23, 2007)

uh oh, now is doesn't count anymore, it says it cannot load the txt file.


----------



## dez_666 (May 30, 2007)

Did you remove ALL of the /a switches or just the set command that reads from the file?


----------



## Newrad0603 (Jul 23, 2007)

just the first /a


----------



## dez_666 (May 30, 2007)

Hmm. Ok i tested the batch by replacing your file you are testing with dir C:
and obviously it will result in a success every time

but ok for each set command, put the /a switch and it should work without errors..it did for me but im on windows 2000 right now :|


----------



## JohnWill (Oct 19, 2002)

I think I'd just use something like AutoIt to automate it, much easier.


----------



## Newrad0603 (Jul 23, 2007)

I'm on win2000 also, but it is still saying missing operand


----------



## dez_666 (May 30, 2007)

lol JohnWill, ruining my scripting fun


----------



## JohnWill (Oct 19, 2002)

I'm a *Cut to the chase* kind of guy.


----------



## dez_666 (May 30, 2007)

Well hey, I am bored, I am at work, I needed something to do xD
Other than play nethack over remote desktop that is


----------



## Newrad0603 (Jul 23, 2007)

I fixed the missing operand problem, the equal sign was doing it,

thanks for all the help, and all the patience


----------



## JohnWill (Oct 19, 2002)

*You can mark your own threads solved using the thread tools at the top of the page in the upper right corner.©*


----------

