Sending variables between files with batch












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










share|improve this question

























  • 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.

    – 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 at 7:07
















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










share|improve this question

























  • 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.

    – 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 at 7:07














1












1








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










share|improve this question
















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







batch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 8:53







user976779

















asked Dec 31 '18 at 14:42









user976779user976779

185




185













  • 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.

    – 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 at 7:07



















  • 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.

    – 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 at 7:07

















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





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.

– Pimp Juice IT
Dec 31 '18 at 18:45





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.

– 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 at 7:07





@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 at 7:07










2 Answers
2






active

oldest

votes


















2














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%





share|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.

    – 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 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 "sharec"
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:xc*.*"); do set c="%~ni"


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






share|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








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1389276%2fsending-variables-between-files-with-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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%





share|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.

    – 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 at 18:33


















2














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%





share|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.

    – 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 at 18:33
















2












2








2







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%





share|improve this answer













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%






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 15:49









5p0ng3b0b5p0ng3b0b

667




667













  • 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.

    – 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 at 18:33





















  • 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.

    – 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 at 18:33



















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





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.

– Pimp Juice IT
Dec 31 '18 at 18:46





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.

– 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 at 18:33







The 'reg query' command can be used to read registry values once a batch file is already running.

– 5p0ng3b0b
Jan 2 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 "sharec"
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:xc*.*"); do set c="%~ni"


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






share|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
















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 "sharec"
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:xc*.*"); do set c="%~ni"


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






share|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














0












0








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 "sharec"
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:xc*.*"); do set c="%~ni"


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






share|improve this answer















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 "sharec"
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:xc*.*"); do set c="%~ni"


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







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 15:53

























answered Dec 31 '18 at 14:48









cybernardcybernard

10k31525




10k31525













  • @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



















  • @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

















@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





@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


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1389276%2fsending-variables-between-files-with-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Plaza Victoria

In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...