Batch file if statement set variable




















Collectives on Stack Overflow. Learn more. Set variable inside IF statement windows batch file Ask Question. Asked 4 years, 11 months ago. Active 4 years, 11 months ago. Viewed 29k times. Any ideas? Thanks in advance! Improve this question. Jyclop Jyclop 1 1 gold badge 3 3 silver badges 14 14 bronze badges. To get actual values you need to force delayed expansion. Add a comment. Active Oldest Votes. Super User is a question and answer site for computer enthusiasts and power users.

It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search. This shouldn't have anything to do with the IF, clearly the block is being executed. Environment variables in batch files are expanded when a line is parsed. In the case of blocks delimited by parentheses as your if defined the whole block counts as a "line" or command.

In your case with nothing, as the variable doesn't have a value yet. Delayed expansion causes variables delimited by exclamation marks! Finally, support for delayed environment variable expansion has been added.

Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed.

The following example demonstrates the problem with immediate variable expansion:. So the IF inside the compound statement is really comparing "before" with "after" which will never be equal. Similarly, the following example will not work as expected:. So the actual FOR loop we are executing is:. Delayed environment variable expansion allows you to use a different character the exclamation mark to expand environment variables at execution time.

If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:. Joey's explanation is my favorite. Note however that enabledelayedexpansion does not work on Windows NT 4. About your follow-up question, no, it is not possible to EnableDelayedExpansion without setlocal.

However the original behavior that was going against you can be used to workaroud that second problem: the trick is to endlocal on the same line where you set again the values of the variables you need. But here is another workaround to that problem: use a procedure in the same file instead of an inline block or an external file.

If it isn't working that way, you likely have delayed environment variable expansion on. This happens because your line with FOR command evaluates only once. You need some way to reevaluate it. You could simulate a delayed expansion with CALL command:. Following is a screenshot of how this would look in the command prompt when the batch file is executed.

Following is the syntax of the set command. In the above code snippet, a variable called message is defined and set with the value of "Hello World". In batch script, it is also possible to define a variable to hold a numeric value. All of the arithmetic operators work in batch files.

The following example shows arithmetic operators can be used in batch files. In any programming language, there is an option to mark variables as having some sort of scope, i.



0コメント

  • 1000 / 1000