0

I have this problem that I want to add the content of one parameter to another. So I will explain this to you. This is some of my code:

set /a A=%A1%+%A2%+%A3%+%A4%+%A5%+%A6%+%A7%+%A8%+%A9%
set /a B=%B1%+%B2%+%B3%+%B4%+%B5%+%B6%+%B7%+%B8%+%B9%
set /a C=%C1%+%C2%+%C3%+%C4%+%C5%+%C6%+%C7%+%C8%+%C9%
set /a D=%D1%+%D2%+%D3%+%D4%+%D5%+%D6%+%D7%+%D8%+%D9%
set TestTheAnswer= The following lines are wrong: 
if %A% NEQ 45 (then it should add "A, " to %TestTheAnswer% and of course the same with the other ones.)
echo %TestTheAnswer%

And at the end it should be like: "The following lines are wrong: A, B, D ,". I have already an option how to do this in my mind but it would be very complicated... So can anyone help me with this? :) regards

  • 1
    *"I have already an option how to do this in my mind but it would be very complicated."* - Please elaborate. – GolezTrol Feb 05 '16 at 14:14

3 Answers3

0

just add A, to the variable:

...
set "wrongs= "
if %A% NEQ 45 set "wrongs=%wrongs%A, "
if %B% NEQ 45 set "wrongs=%wrongs%B, "
if %C% NEQ 45 set "wrongs=%wrongs%C, "
if %D% NEQ 45 set "wrongs=%wrongs%D, "
echo The following lines are wrong:%wrongs%
Stephan
  • 53,940
  • 10
  • 58
  • 91
0
@ECHO OFF
SETLOCAL
FOR /L %%a IN (1,1,9) DO SET /a A%%a=%%a * 2
SET /a total=0
FOR /L %%a IN (1,1,9) DO SET /a total+=A%%a

SET a
ECHO %total%

GOTO :EOF

I haven't the first idea what 45 has to do with the price of fish. What you appear to be doing is totalling A1..A9, B1..B9 .. D1..D9 then doing nothing with the totals of B..D

Still, here's an easy way to do the totalling. I've set A1..A9 to a set of numbers, then totalled them and displayed the total along with the values installed in A*.

HTH

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Ok. there are several points here; the first one is that the set /A command may work with just the names of variables, so all your variable expansions are not needed. This should work:

set /a A=A1+A2+A3+A4+A5+A6+A7+A8+A9
set /a B=B1+B2+B3+B4+B5+B6+B7+B8+B9
set /a C=C1+C2+C3+C4+C5+C6+C7+C8+C9
set /a D=D1+D2+D3+D4+D5+D6+D7+D8+D9
set "TestTheAnswer= The following lines are wrong:"
if %A% NEQ 45 set "TestTheAnswer=%TestTheAnswer% A,"
if %B% NEQ 45 set "TestTheAnswer=%TestTheAnswer% B,"
if %C% NEQ 45 set "TestTheAnswer=%TestTheAnswer% C,"
if %D% NEQ 45 set "TestTheAnswer=%TestTheAnswer% D,"
echo %TestTheAnswer:~0,-1%

However, a collection of variables with same name and a changing subscript is called "array", and the advantages of arrays is that you don't need to explicitly write each element of the array, but just write one element and change the subscript via a for command. Also, you may make good use of the fact that set /A command allows to perform several operations separating each one with a comma:

setlocal EnableDelayedExpansion

for /L %%i in (1,1,9) do set /A A+=A%%i, B+=B%%i, C+=C%%i, D+=D%%i
set "TestTheAnswer= The following lines are wrong:"
for %%v in (A B C D) do if !%%v! NEQ 45 set "TestTheAnswer=!TestTheAnswer! %%v,"
echo %TestTheAnswer:~0,-1%

For further details on array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

PS - Your question is about "add several variables to one variable"; a parameter is a different thing. I suggest you to change the topic title.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108