Home > Back-end >  Adding numbers from text file in batch script
Adding numbers from text file in batch script

Time:11-10

I've got a problem with batch script. I have to make script which adds numbers located within a text file.

The numbers are in one line of the file and separated by spaces.

I have to do it using a for loop.

I've made this:

setlocal EnableDelayedExpansion
set /a wynik=0
for /f "tokens=1* delims= " %%a in (liczby.txt) do (set /a wynik =%%a)
echo %wynik% > wynik.txt

The output is:

C:\Users\NuClear\Desktop\arch\2>setlocal EnableDelayedExpansion

C:\Users\NuClear\Desktop\arch\2>set /a wynik=0

C:\Users\NuClear\Desktop\arch\2>for /F "tokens=1* delims= " %a in (liczby.txt) do (set /a wynik =%a )

C:\Users\NuClear\Desktop\arch\2>(set /a wynik =1 )

C:\Users\NuClear\Desktop\arch\2>echo 1  1>wynik.txt

I've got no idea why it is not taking next tokens after 1.

CodePudding user response:

You didn't specify if you have to use for to read the file or just for the adding part.

REM read the (first line of the) file into a variable:
<liczby.txt set /p "line="
REM or using a FOR /F to read the file:
REM for /f "delims=" %%a in (liczby.txt) do set "x=%%a"
REM add them up:
set "y=0"
for %%a in (%line%) do set /a y =%%a
echo Sum: %y%

for /f with tokens is extremely helpful, if you know beforehand, how many tokens there are. A plain for just executes for each token (using standard delimiters), how many there may be.

CodePudding user response:

Here is another option if you want to do it with a single FOR command. The key to this option is using CMD.exe with the /U option.

 @echo off
 set "wynik=0"
 for /F "delims=" %%G in ('cmd /D /U /C type liczby.txt ^| find /V ""') do (
    set /A "wynik =%%G" 2>nul
 )
 >wynik.txt echo %wynik%

CodePudding user response:

A simpler method:

@echo off
setlocal

set /P "line=" < liczby.txt
set /A "wynik=%line: = %"
> wynik.txt echo %wynik%

This method assumes that "The numbers are in one line of the file and separated by one space each", with no spaces at beginning or end of the line.

CodePudding user response:

It appears to me, from the information you've provided, that the simplest idea would be to replace the single spaces with the addition operator.

Here's an example written for your tag:

@(
    For /F "UseBackQ Delims=" %%G In (
        "liczby.txt"
    ) Do @(
        Set "}=%%G"
        For /F "Delims=" %%H In (
            '%SystemRoot%\System32\cmd.exe /V /D /C "Set /A "!}: ^= !""'
        ) Do @Echo(%%H
    )
) 1>"wynik.txt"

And for your tag:

(For /F "UseBackQ Delims=" %G in ("liczby.txt") Do @Set "}=%G" & For /F "Delims=" %H In ('%SystemRoot%\System32\cmd.exe /V /D /C "Set /A "!}: ^= !""') Do @Echo(%H) 1>"liczby.txt"

The current responses have possibly read into your question in a different way, due to its lack of clarity or examples, so my answer above assumes that liczby.txt looks a little like this:

1 2 3 4 5
6 7 8 9
10 11 12
13 14

…and the resultant wynik.txt would look something like this:

15
30
33
27

CodePudding user response:

I assume you have got a text file liczby.txt with a content like this:

1 2 3

When you read it with a for /F loop there is going to be one iteration because there is only a single line. Within this iteration all the tokens specified by the option string are returned in respective meta-variables.

But what you need is something else, you want to iterate through the individual numbers, which can be achieved using a standard for loop:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Initialise sum:
set /A "wynik=0"
rem // Read the input text file line by line:
for /F "usebackq tokens=*" %%J in ("liczby.txt") do (
    rem // Loop through individual items/numbers:
    for %%I in (%%J) do (
        rem // Sum up the numbers:
        set /A "wynik =%%I"
    )
)
rem // Write sum into output file:
> "wynik.txt" echo(%wynik%
endlocal
exit /B
  • Related