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.
forloop 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 as1 2 3 4 5, the first token,%a, will be1, and the second token,%b, will be2 3 4 5. Please open a Command Prompt window, typefor /?and read through the help and usage information of the command you are wanting to use, to better understand how to do so.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%?