Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Sum of all the numbers in a file using Batch  (Read 8408 times)

0 Members and 1 Guest are viewing this topic.

zahirkhan07

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    Sum of all the numbers in a file using Batch
    « on: October 12, 2011, 07:29:39 AM »
    Hello all,

    I am a newbie to the batch scripting and I need some suggestions to get my batch file code as minimum as possible.

    I have a text file names "runs.log" which contains some numbers, like this:

    0
    12
    634
    194
    1781
    63


    What I need is the batch script will open the file and summarize all the numbers and give me the result.

    I am trying to use the following code, but its not working :(


    for /f %%a in (runs.log) do (
       set /P MR=<MONTHLY_RUN.log
       set /a DAILY=%%a+%MR%\
       echo %DAILY%>MONTHLY_RUN.tmp
    )

    Thanks to any kind of help

    Raven19528



      Hopeful
    • Thanked: 30
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Sum of all the numbers in a file using Batch
      « Reply #1 on: October 12, 2011, 10:17:39 AM »
      There are a few things I can see here. First, your code seems to be doing more than just giving you the result, which begs the question "Are you gathering a daily runs.log and adding it to a monthly rather than just gathering a daily total?" To do what you originally asked for which is to summarize what is in the runs.log file. try this:

      Code: [Select]
      for /f %%a in (runs.log) do (set /a total+=%%a)
      That will give you a total of your runs.log, which, if that is your daily total, can then be added to your monthly total and the log. I'm curious as to how this all interplays with eachother. To me it seems the runs.log keeps some sort of running tally of instances, and then you are trying to add the daily total to your monthly total and keep a log of how things are progressing. If that is in fact the case, try this:

      Code: [Select]
      @echo off
      for /f %%A in (MONTHLY_RUN.log) do (set MR=%%A)
      for /f %%B in (runs.log) do (set /a total+=%%B)
      set /a DAILY=%MR%+%total%
      echo %DAILY%>>MONTHLY_RUN.log

      This grabs the previous total from MONTHLY_RUN.log, adds the total from runs.log, and puts a new line with the new total back into MONTHLY_RUN.log. If that isn't what you are trying to do, then disregard. :)
      "All things that are
      Are with more spirit chased than enjoy'd" -Shakespeare

      zahirkhan07

        Topic Starter


        Newbie

        • Experience: Beginner
        • OS: Unknown
        Re: Sum of all the numbers in a file using Batch
        « Reply #2 on: October 13, 2011, 01:28:55 AM »
        Thank you very much  Raven19528
        it really works.. you saved my day :D  :D