Add Comment
|
Related Links
|
TrackBack
Multi-Line IF Statements in Batch FileMost folks don't realize it, but you can actually place multiple statements into if-else clauses. In fact, most people don't even realize that the batch file allows you to have an else clause. The following code demonstrates how you can have multi-line IF statements, so that you don't have to do a lot of goto's. if exist %sourceDir% ( The '(' and ')' must be on the same line as the if and else statements as shown above. Even though they are not the open and close bracket, you can think of them as such. With multi-line if-else statements, you batch file can become far more structured than ever. Chieh Cheng I hit a snag today. The set variable function does not work inside a multi-statement if-else scope. For example, the following code does not work: if "%tempDir%" == "" ( The batch processor was never able to use the %output% that was set within that scope. Chieh Cheng The work around for this problem is to call a sub-routine in the if statement. The following is an example: if "%tempDir%" == "" ( Chieh Cheng Have you tried to at the beginning of your code set SetLocal EnableDelayedExpansion and before exiting from the script code EndLocal johan k Just tried it, johan. That didn't work. :-( The following is the prototype code: SetLocal EnableDelayedExpansion Instead of printing "*** just some text", the output was "ECHO is off." . . . meaning %output% wasn't assigned a value. Chieh Cheng Hey Cheih, echo.%output% It unsures that the echo output command doesn't get confused with the echo on/off command. Seems like it's reading your output variable and defaulting ECHO to off because it's not a valid parameter (on or off). You've probably got over this problem by now, but this may help someone else. luke mackay Try: echo !output! Replacing the % with ! will solve your problem with setting a variable in a loop. TheBlackOne Thanks, TheBlackOne! That worked. Delayed expansion has to be enabled for this trick to work. The following is an example batch program that demonstrates this trick. SetLocal EnableDelayedExpansion Chieh Cheng I want to add to the previous comment that you can use the new variable within the IF clause outside the IF clause scope. The following example expands on the previous one and shows how it can be used outside the scope. SetLocal EnableDelayedExpansion Chieh Cheng You are great! Ivan Ferrer Found another interesting side-effect. If "!output!" is assigned within the if-clause, then "%output%" will be assigned after exiting the if-clause scope. Use following example: SetLocal EnableDelayedExpansion The output is as follows: ECHO is off. Notice that "%output%" cannot be reference in the if-clause scope, but it can be referenced in the global scope. Chieh Cheng Thanks for teaching me the call subroutine command. If statements are quite fickle and cmd batch scripts, sometime collapsing the script for no reason. Simplifying the if statement by making it call a subrountine really helped. Fadi R
Add Comment
|
Related Links
|
TrackBack
Did your message disappear? Read the Forums FAQ. TrackBackTrackBack only accepted from WebSite-X Suite web sites. Do not submit TrackBacks from other sites. Send Ping | TrackBack URL | Spam Control Title: SetVariableToOutput.bat (GPL) Add CommentSpam Control | * indicates required field Messages, files, and images copyright by respective owners. |
Products
|
Services
106 Users Online
|
Copyright © 1996 - 2024. All Rights Reserved. |