Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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

share|improve this answer
    
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 '13 at 1:55
add comment

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

share|improve this answer
    
Thanks for the tips, and neat explanation. I got the point now. Appreciate your help! –  tsum Nov 28 '13 at 1:56
    
@tsum, you can vote and accept if you feel so ;) –  Free Consulting Nov 28 '13 at 3:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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