1

So i have 2 Bat files A and B. A runs, now I use a command to start B. Now A and B are running at the same time. And now I want to send my Variable C from A to B. I tried Call but then it just opens B twice . Thanks for future answeres! Edit* without additional files

  • Please make sure there is no XY problem here. If there is however, then edit and explain the real goal. – Kamil Maciorowski Dec 31 '18 at 15:04
  • I think you will have to control the logic well in B and set an initial set /p from a file like shown in Sponge Bob's answer to a default when initially launched and add conditional logic in it to tell it to only take whatever action you want it to take if not ["%var%"]==["%setVar%"] and then if it does not equal that setVar to then so something and set the setVar to whatever you just ran and then starting checking the conditional logic from the value in the file until it changes again which A can do whenever. This is certainly possible that way working around the limitation in a way. – Formerly Pimp Juice IT Dec 31 '18 at 18:45
  • @DavidPostill I believe waitfor is a highly limited IPC . At least between batch files. It may be an answer if this is a mistated question but in the way this has been asked it is not an answer – Ross Jan 1 '19 at 7:07
3

The setx command writes a variable into the registry and can be used if you want to use a variable globally for all batch files.

SETX VAR_C somevalue

Alternatively you could write the variable to a file and read it back into the other batch file ie;

Batch file A:

SET VAR_C=somevalue
ECHO %VAR_C% >%TMP%\var_c

Batch file B:

SET /P VAR_C=<%TMP%\var_c
ECHO %VAR_C%
| improve this answer | |
  • Setx won't help with already running batches. A batch inherits the environment from the starting process and will not recognize meanwhile changes to the master environment. – LotPings Dec 31 '18 at 17:25
  • I think you will have to control the logic well in B and set an initial set /p from a file like shown in Sponge Bob's answer to a default when initially launched and add conditional logic in it to tell it to only take whatever action you want it to take if not ["%var%"]==["%setVar%"] and then if it does not equal that setVar to then so something and set the setVar to whatever you just ran and then starting checking the conditional logic from the value in the file until it changes again which A can do whenever. This is certainly possible that way working around the limitation in a way. – Formerly Pimp Juice IT Dec 31 '18 at 18:46
  • The 'reg query' command can be used to read registry values once a batch file is already running. – 5p0ng3b0b Jan 2 '19 at 18:33
0

If you were not using the windows command prompt running a and b at the same time is impossible. Even with the call command a is stop and b runs to completion return to a after the call command in true DOS.

When running in windows command prompt they are 2 entirely different processes and don't share memory, variables, or etc.

The way batch files work is they use %1 to %9

shift allows the use of 10 or more values by shifting everything down by 1 and losing the lowest value.

So inside of a.bat call b.bat "scooby doo" "was" "here"

Now inside of b.bat

%1 = "scooby doo"
%2 = "was"
%3 = "here"

alternative

The SET command also exists

set C="scooby doo"

echo %c%


From the department of ugly,limited, and hacky

mkdir "\share" mkdir "\share\c" and then in batch file a you could create a file named whatever you want to share.

note: inside a batch file double percent symbols maybe be needed.

for  %i in ("c:\x\c\*.*"); do set c="%~ni"

However, you can't use the same special characters then, for example the period.

| improve this answer | |
  • @KamilMaciorowski I have added an alternative. However DOS is very limited in what it can do. Depending on how b was started neither way is guaranteed to work. Its best if b isn't started before hand then the variables will transition. If a and b are started in 2 different windows command prompts they are entirely different processes and don't share any memory space. – cybernard Dec 31 '18 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.