2

I would like to set a variable in another batch file, if it exsists. But it works only localy in the sub batch file. How can I fix this problem?

Main.bat:

SET TEMP=""
IF EXIST SUB.bat (
    CALL SUB.bat 
      REM Returns: TEMP="" IN MAIN
      ECHO %TEMP% IN MAIN
) ELSE (
      SET TEMP="DEFAULT VALUE"
)

Sub.bat:

SET TEMP="OTHER VALUE"
REM Returns: TEMP="OTHER VALUE" IN SUB
ECHO %TEMP% IN SUB

Output by calling Main.bat:

TEMP="OTHER VALUE" IN SUB
TEMP="" IN MAIN

1 Answer 1

1

Two issues:

Your test is incorrect. Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

Try using CALL ECHO %%TEMP%% to display the altered value and look up "delayedexpansion" for endless SO items on this frequently-encountered subject.

Second issue - which impinges on the first.

TEMP and TMP are special variablenames which specify the location of a temporary-files directory. Best not to change them as unexpected results may ensue. Use another variablename.

Sign up to request clarification or add additional context in comments.

3 Comments

But the variable is empty, if I use it in another Batch file, which has been called by the Main.bat. Why?
Without the exact code you are using, any response would be a guess unfortunately.
I don't know what I did or if I did anything but it works now -.- THX!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.