0

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.

7
  • 3
    You have asked your for loop to parse the output as two individual tokens separated by the space delimiter, the first token is the first non space character string on the line, passed as %a, the second token is all other characters following the space character after that first string, passed as %b. So if your line reads as 1 2 3 4 5, the first token, %a, will be 1, and the second token, %b, will be 2 3 4 5. Please open a Command Prompt window, type for /? and read through the help and usage information of the command you are wanting to use, to better understand how to do so. Commented Nov 9, 2021 at 10:56
  • 1
    What about this: for /F "tokens=*" %%J in (liczby.txt) do set /A "wynik=0" & for %%I in (%%J) do set /A "wynik+=%%I", then > wynik.txt echo/%wynik%? Commented Nov 9, 2021 at 12:25
  • @aschipfl %%J was unexpected at this time Commented Nov 9, 2021 at 12:42
  • @Compo i've tried to understand for /? but still i cannot make it. as %a stands for first element without rest, why %b cannot be the second one and so on Commented Nov 9, 2021 at 12:43
  • 1
    NuClear1331 I had no problems using @aschipfl 's code in a batch file. Commented Nov 9, 2021 at 13:43

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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%

1 Comment

What if a number has got more than a single digit?
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.