how to access variables that are set in a different windows batch file?

4

i have a windows batch file - called fileA.bat, which contains a variable set in it. Sample code of fileA.bat:

set fileA_username=michael

now i have another batch file, called fileB.bat, which needs to access this variable set in fileA.bat.

So the code that I write in fileB.bat would want to do something like this:

set fileB_username = %fileA_username%

and then keep using the fileB_username in the script code.

FileB would be calling fileA as part of execution like this:

code in fileB:

---
---
call fileA.bat
---
---
set fileB_username = fileA_username
---
if fileB_username
----

how do i access variables that are set in external batch files? Thanks.

tsum

Posted 2013-11-27T10:57:36.377

Reputation: 41

Answers

3

You've got a typo in the variable name: fielA_username vs. fileA_username, otherwise your code is good. Also you should avoid pretty spaces around = sign in SET command since they will become a part of variable name and its value (try: SET foo=bar && SET foo = bar && SET foo).


@REM this is setenv.bat

SET ENV=IRON

@REM and this is "main" script

CALL setenv.bat

@REM append something to the value
SET ENV=%ENV%MENT

REM print the current value
SET ENV

Free Consulting

Posted 2013-11-27T10:57:36.377

Reputation: 220

Thanks for the tips, and neat explanation. I got the point now. Appreciate your help! – tsum – 2013-11-28T01:56:31.427

@tsum, you can vote and accept if you feel so ;) – Free Consulting – 2013-11-28T03:08:52.907

2

Unless you specify SETLOCAL or unset the variable explicitly in batch file A, the variable will be global and accessible to your second batch file.

For further information see Why unload variables in Windows batch files

Der Hochstapler

Posted 2013-11-27T10:57:36.377

Reputation: 77 228

thanks for the clarification. And the link you gave provided a very concise explanation into what I was looking for. Really appreciate your help. Thanks! – tsum – 2013-11-28T01:55:01.700