6

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.

2 Answers 2

8

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

2
  • Thanks for the tips, and neat explanation. I got the point now. Appreciate your help!
    – tsum
    Nov 28, 2013 at 1:56
  • @tsum, you can vote and accept if you feel so ;) Nov 28, 2013 at 3:08
4

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

1
  • 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
    Nov 28, 2013 at 1:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .